作者的版本:
layout (main.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Switch android:id="@+id/switchdemo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="WTF?" /> </LinearLayout>
java代码:
public class SwitchActivity extends Activity implements CompoundButton.OnCheckedChangeListener { Switch sw; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); sw=(Switch)findViewById(R.id.switchdemo); sw.setOnCheckedChangeListener(this); } // @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { sw.setTextOn("This switch is: on"); } else { sw.setTextOff("This switch is: off"); } } }
作者提供的 java 代码有问题. 原因参考(http://stackoverflow.com/questions/29811646/android-settexton-not-working-in-oncheckchanged-method)
I don't think your calls to setTextOn and setTextOff need to be in an if - they just define how the toggle appears when on or off, so they don't need to be set conditionally. Ref: API – Simon MᶜKenzie Apr 23 '15 at 1:01
The
setTextOn
andsetTextOff
functions are to used to set the labels depending on the state of theSwitch
.The text "The switch is: On" is just the label of your Switch and does not convey the state of your
Switch
.To achieve the result that you want, you need to call
setShowText(true)
:sw = (Switch)findViewById(R.id.swish); sw.setShowText(true);
or you can add it in your 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"/>
As observed by @Simon M, this xml and java snippet produce consistent output as shown by the screen below.
<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 });
答案说的很清楚了. 只需要在 onCreate 里面调用 setTextOn/Off 即可. onCheckedChanged 什么都不用做. 或者根本步调用 setTextOn/Off, 直接在 main.xml 中用 `android::textOn/Off`即可.
下面是修改后的 java 实现, 没有修改 main.xml, 也展示了 setText 与 setTextOn/Off 的区别.
public class SwitchActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { Switch sw; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); sw=(Switch)findViewById(R.id.switchdemo); sw.setOnCheckedChangeListener(this); sw.setTextOff("#OFF#"); sw.setTextOn("#ON#"); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { sw.setText("I'm On."); } else { sw.setText("I'm Off."); } } }