作者的版本:

layout (main.xml):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <Switch
  7. android:id="@+id/switchdemo"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="WTF?" />
  11. </LinearLayout>

java代码:

  1. public class SwitchActivity extends Activity
  2. implements CompoundButton.OnCheckedChangeListener {
  3. Switch sw;
  4.  
  5. @Override
  6. public void onCreate(Bundle icicle) {
  7. super.onCreate(icicle);
  8. setContentView(R.layout.main);
  9.  
  10. sw=(Switch)findViewById(R.id.switchdemo);
  11. sw.setOnCheckedChangeListener(this);
  12. }
  13.  
  14. // @Override
  15. public void onCheckedChanged(CompoundButton buttonView,
  16. boolean isChecked) {
  17. if (isChecked) {
  18. sw.setTextOn("This switch is: on");
  19. }
  20. else {
  21. sw.setTextOff("This switch is: off");
  22. }
  23. }
  24. }

作者提供的 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):

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

or you can add it in your XML.

  1. <Switch
  2. android:layout_width="453dp"
  3. android:layout_height="100dp"
  4. android:id="@+id/swish"
  5. android:layout_gravity="center_vertical"
  6. android:layout_alignParentTop="true"
  7. android:showText="true"/>

As observed by @Simon M, this xml and java snippet produce consistent output as shown by the screen below.

  1. <Switch
  2. android:layout_width="453dp"
  3. android:layout_height="100dp"
  4. android:id="@+id/swish"
  5. android:layout_gravity="center_vertical"
  6. android:textOn="ON!!"
  7. android:textOff="OFF!"
  8. android:layout_alignParentTop="true"
  9. android:layout_centerHorizontal="true"/>
  10. sw = (Switch)findViewById(R.id.swish);
  11. sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  12. @Override
  13. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  14. // absolutely nothing
  15. });
  1.  

答案说的很清楚了. 只需要在 onCreate 里面调用 setTextOn/Off 即可. onCheckedChanged 什么都不用做. 或者根本步调用 setTextOn/Off, 直接在 main.xml 中用 `android::textOn/Off`即可.

下面是修改后的 java 实现, 没有修改 main.xml, 也展示了 setText 与 setTextOn/Off 的区别.

  1. public class SwitchActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {
  2.  
  3. Switch sw;
  4. @Override
  5. public void onCreate(Bundle icicle) {
  6. super.onCreate(icicle);
  7. setContentView(R.layout.main);
  8. sw=(Switch)findViewById(R.id.switchdemo);
  9. sw.setOnCheckedChangeListener(this);
  10. sw.setTextOff("#OFF#");
  11. sw.setTextOn("#ON#");
  12. }
  13.  
  14. public void onCheckedChanged(CompoundButton buttonView,
  15. boolean isChecked) {
  16. if (isChecked) {
  17. sw.setText("I'm On.");
  18. } else {
  19. sw.setText("I'm Off.");
  20. }
  21. }
  22. }

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. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...

  2. 转:jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  3. HDU 4749 Parade Show(贪心+kmp)

    题目链接 题目都看不懂,做毛线...看懂了之后就是kmp出,所有的匹配区间,然后DP可以写,贪心也可以做把,DP应该需要优化一下,直接贪,也应该对的,经典贪心问题. #include<iostr ...

  4. 20145330《Java程序设计》第二次实验报告

    20145330<Java程序设计>第二次实验报告 实验二 Java面向对象程序设计 实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承多态 3.初步掌握UM ...

  5. SolrCloud 5.x 集群部署方法

    CentOS下安装Solr5.3    http://www.centoscn.com/image-text/install/2015/0918/6190.html solr5.3.1 集群服务搭建 ...

  6. Node.js前端自动化工具:gulp

    前端自动化工具 -- gulp 使用简介 gulp是基于流的前端自动化构建工具. 之前也谈到了 grunt的用法,grunt其实就是配置+配置的形式. 而gulp呢,是基于stream流的形式,也就是 ...

  7. Maya 脚本控制物体自转

    在Maya中,我们可以用脚本来控制物体的自转方向,速度等等,步骤如下: 选择需要操作的物体object,打开通道盒Channel Box,点击编辑Edit,打开表达式Expressions面板 选择需 ...

  8. [CareerCup] 15.5 Denormalization 逆规范化

    15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助 ...

  9. django向数据库添加数据

    url.py views.py host.html (样式) (展示部分) (添加信息界面) (js部分) 展示添加数据:

  10. 获取客户端真实ip

    // 获取客户端真实ip() protected function getIP() { global $ip; if (getenv("HTTP_CLIENT_IP")) $ip ...