2015-04-23 25 views
2

下面两个语句产生正确屏幕左下:的Android setTextOn不onCheckChanged方法工作

   if(isChecked) sw.setText ("This switch is: On"); 
       else   sw.setText ("This switch is: Off"); 

根据我在看文字,这接下来的两个语句应该农产品屏上留下的。但是,他们生产不正确屏幕右下:

   if(isChecked) sw.setTextOn ("This switch is: On"); 
       else   sw.setTextOff("This switch is: Off"); 

enter image description hereenter image description here

代码:

package com.dslomer64.checkbox; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.Switch; 


public class MainActivity extends ActionBarActivity{//} implements CompoundButton.OnCheckedChangeListener { 
    CheckBox cb; 
    Switch sw; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     cb = (CheckBox)findViewById(R.id.check); 
     cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) 
        cb.setText("checked"); 
       else 
        cb.setText("Unchecked"); 
      } 
     }); 

     sw = (Switch)findViewById(R.id.swish); 
     sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked) 
        sw.setTextOn("This switch is: On"); //////////// 
       else 
        sw.setTextOff("This switch is: Off"); ////////// 

      } 
     }); 
    } 
} 

的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       tools:context=".MainActivity" android:weightSum="1"> 

    <Switch 
     android:layout_width="453dp" 
     android:layout_height="100dp" 
     android:id="@+id/swish" 
     android:layout_gravity="center_vertical" 
     android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/> 

    <CheckBox 
     android:text="Unchecked" 
     android:layout_width="200dp" 
     android:layout_height="100dp" 
     android:id="@+id/check" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

</RelativeLayout> 

是书错setTextOnsetTextOff?我在java代码或xml中遇到问题吗?

+2

我不认为你对'setTextOn'和'setTextOff'的调用需要在'if'中 - 它们只是定义了在开启或关闭时如何显示切换,所以它们不需要有条件地设置。参考:[API](http://developer.android.com/reference/android/widget/ToggleButton.html#setTextOff%28java.lang.CharSequence%29) –

回答

2

setTextOnsetTextOff函数用于根据Switch的状态设置标签。

文本“开关是:开”只是您的开关的标签,并不代表传达您的Switch的状态。

为了达到你想要的结果,你需要调用setShowText(true)

sw = (Switch)findViewById(R.id.swish); 
sw.setShowText(true); 

,或者你可以在你的XML添加。

<Switch 
     android:layout_width="453dp" 
     android:layout_height="100dp" 
     android:id="@+id/swish" 
     android:layout_gravity="center_vertical" 
     android:layout_alignParentTop="true"  
     android:showText="true"/> 
1

正如@Simon M所观察到的,这个xml和java片段会产生一致的输出,如下面的屏幕所示。

<Switch 
    android:layout_width="453dp" 
    android:layout_height="100dp" 
    android:id="@+id/swish" 
    android:layout_gravity="center_vertical" 
    android:textOn="ON!!" 
    android:textOff="OFF!" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"/> 



    sw = (Switch)findViewById(R.id.swish); 
    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // absolutely nothing 
     }); 

enter image description here

编辑

下面的XML和Java产生什么样的案文瞄准:

<Switch 
    android:layout_width="453dp" 
    android:layout_height="100dp" 
    android:id="@+id/swish" 
    android:layout_gravity="center_vertical" 
    android:layout_alignParentTop="true" 
    android:text="This switch is...OFF" 
    android:checked="false" 
    android:layout_centerHorizontal="true"/> 



    sw = (Switch)findViewById(R.id.swish); 
    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if(isChecked) 
       sw.setText("Switch is ON!"); 
      else 
       sw.setText("Switch is OFF!"); 
     } 
    }); 

...这是这样的:

enter image description here