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