作者的版本:

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 and setTextOff functions are to used to set the labels depending on the state of the Switch.

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.");
}
}
}

最新文章

  1. JAVA算法
  2. 用js效果做的简单焦点图
  3. 慕课网-安卓工程师初养成-5-4 使用 Eclipse 调试程序
  4. HDU 3999 The order of a Tree
  5. IT公司100题-1-二叉树转换为双链表
  6. Google 宣布支持中文邮箱地址
  7. bzoj2180: 最小直径生成树
  8. NodeJS Stream 四:Writable
  9. 浅谈JavaScript的apply和call语句
  10. python 使用paramiko模块上传本地文件到ssh
  11. Microsoft dynamic sdk中join应该注意的问题.
  12. 如何正确的理解和解决 ORA-01843:not a valid month
  13. 『Python CoolBook』C扩展库_其四_结构体操作与Capsule
  14. ELK菜鸟手记 (一) 环境配置+log4j日志记录
  15. 4个项目带你学习ThinkPHP
  16. python记录_day01 初始
  17. 代码:jquery自定义插件 demo
  18. vue axios请求/响应拦截器
  19. [转]Kafka 设计与原理详解
  20. Ubuntu16.04(Linux)安装JDK

热门文章

  1. ACM: 敌兵布阵 解题报告 -线段树
  2. Android --RatingBar的使用
  3. vim安装插件
  4. 最好的文本框样式 最漂亮的文本框样式 textbox css样式
  5. 运行java的class文件方法详解
  6. JS代码判断字符串中有多少汉字【转】
  7. XML 参考:XML基础 XML 简介
  8. Debug与Release的区别
  9. load/get延迟加载和及时加载
  10. JS写四个图片滚动显示的效果