作者的版本:

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

Beginning Android 4 中 Demo Basic/Switch 的问题.的更多相关文章

  1. Android Studio中Switch控件有关 thumb 和 track 用法

    •任务 •属性 android:track:底部的图片(灰->绿) android:thumb:设置 Switch 上面滑动的滑块,也就是上图中的白色圆形滑块 •switch_thumb 点击 ...

  2. 如何在Android Studio中使用Gradle发布项目至Jcenter仓库

    简述 目前非常流行将开源库上传至Jcenter仓库中,使用起来非常方便且易于维护,特别是在Android Studio环境中,只需几步配置就可以轻松实现上传和发布. Library的转换和引用 博主的 ...

  3. MVP模式在Android开发中的应用

    一.MVP介绍      随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互.同一 ...

  4. Android编程中的5种数据存储方式

    Android编程中的5种数据存储方式 作者:牛奶.不加糖 字体:[增加 减小] 类型:转载 时间:2015-12-03我要评论 这篇文章主要介绍了Android编程中的5种数据存储方式,结合实例形式 ...

  5. 【Android】打电话Demo及Android6.0的运行时权限

    新手开局,查看一些旧资料,从打电话.发短信的小应用开始.代码很简单,主要是学习了: 用StartActivity()激活一个Activity组件.这里是激活了系统原生的打电话和发短信Activity. ...

  6. Intent 对象在 Android 开发中的应用

    转自(http://www.ibm.com/developerworks/cn/opensource/os-cn-android-intent/) Android 是一个开放性移动开发平台,运行在该平 ...

  7. android Service中多线程交互

    android 的service和activity是执行在UI主线程的. 在android线程中,仅仅有主线程即UI线程有自己的默认的消息队列.子线程须要创建自己的消息队列.并把消息发给队列,并循环起 ...

  8. NDK笔记(二)-在Android Studio中使用ndk-build

    前面一篇我们接触了CMake,这一篇写写关于ndk-build的使用过程.刚刚用到,想到哪儿写哪儿. 环境背景 Android开发IDE版本:AndroidStudio 2.2以上版本(目前已经升级到 ...

  9. TensorFlow 在android上的Demo(1)

    转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...

随机推荐

  1. jquery.autocomplete在火狐下的BUG解决

    该插件在火狐下使用拼音输入法输入,回车后无法即使响应事件,需要再输入别的字母才会响应事件     主要原因:   该插件响应主要监听了keydown事件   其他浏览器在打开中文输入法的情况下输入的拼 ...

  2. 再过几个月Apple Watch就要正式发布了

    本文由cocoaChina译者小组成员@TurtleFromMars 翻译自Appcoda,原作者:julian engel,原文:WatchKit Introduction: Building a ...

  3. java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries

    在已经搭建好的集群环境Centos6.6+Hadoop2.7+Hbase0.98+Spark1.3.1下,在Win7系统Intellij开发工具中调试Spark读取Hbase.运行直接报错: ? 1 ...

  4. Windows 下安装使用docker swarm machine docker toolbox

    下载docker 集成安装环境 http://get.daocloud.io/#install-toolbox 这个网站很不错,下载 这个集成了 docker docker-machine ,还有gi ...

  5. Java横向、纵向合并图片

    进行图片对比时候想把两张有差异的图片放到一起,方便人工查看下,在网上搜了一些,有纵向合并的. 将纵向合并的自己调整了下,源码如下: import java.io.File; import java.a ...

  6. 在IE8中使用padding设置select控件文字垂直居中

    在火狐.苹果.谷歌.欧鹏等主流浏览器中,select下拉表单的文字能够垂直居中,如图: 而在ie8中,select下拉表单的文字基本就是靠底部显示,如图: 那么,如何使得ie8下的select文字垂直 ...

  7. [LintCode] Mini Twitter 迷你推特

    Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...

  8. IE hack中主要的几个

    _: IE6; #*+.: IE6 IE7; black\0: IE8; black\9: IE所有; @media screen\9 { … }: IE6 IE7; @media \0screen\ ...

  9. 如何给Sublime安装插件

    第一步:点击链接http://sublime.wbond.net/Package%20Control.sublime-package下载Package Control. 第二步:点击打开Sublime ...

  10. HDU 1018 大数(求N!的位数/相加)

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...