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
setTextOnandsetTextOfffunctions 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.");
}
}
}
Beginning Android 4 中 Demo Basic/Switch 的问题.的更多相关文章
- Android Studio中Switch控件有关 thumb 和 track 用法
•任务 •属性 android:track:底部的图片(灰->绿) android:thumb:设置 Switch 上面滑动的滑块,也就是上图中的白色圆形滑块 •switch_thumb 点击 ...
- 如何在Android Studio中使用Gradle发布项目至Jcenter仓库
简述 目前非常流行将开源库上传至Jcenter仓库中,使用起来非常方便且易于维护,特别是在Android Studio环境中,只需几步配置就可以轻松实现上传和发布. Library的转换和引用 博主的 ...
- MVP模式在Android开发中的应用
一.MVP介绍 随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互.同一 ...
- Android编程中的5种数据存储方式
Android编程中的5种数据存储方式 作者:牛奶.不加糖 字体:[增加 减小] 类型:转载 时间:2015-12-03我要评论 这篇文章主要介绍了Android编程中的5种数据存储方式,结合实例形式 ...
- 【Android】打电话Demo及Android6.0的运行时权限
新手开局,查看一些旧资料,从打电话.发短信的小应用开始.代码很简单,主要是学习了: 用StartActivity()激活一个Activity组件.这里是激活了系统原生的打电话和发短信Activity. ...
- Intent 对象在 Android 开发中的应用
转自(http://www.ibm.com/developerworks/cn/opensource/os-cn-android-intent/) Android 是一个开放性移动开发平台,运行在该平 ...
- android Service中多线程交互
android 的service和activity是执行在UI主线程的. 在android线程中,仅仅有主线程即UI线程有自己的默认的消息队列.子线程须要创建自己的消息队列.并把消息发给队列,并循环起 ...
- NDK笔记(二)-在Android Studio中使用ndk-build
前面一篇我们接触了CMake,这一篇写写关于ndk-build的使用过程.刚刚用到,想到哪儿写哪儿. 环境背景 Android开发IDE版本:AndroidStudio 2.2以上版本(目前已经升级到 ...
- TensorFlow 在android上的Demo(1)
转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...
随机推荐
- STL 的运用 istringstream的运用
单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- CentOS6.4安装mysql2redis
1.安装apr 下载:http://apache.dataguru.cn//apr/apr-1.5.1.tar.gz tar zxvf apr-.tar.gz cd apr- vi configure ...
- 【BZOJ】3771: Triple
http://www.lydsy.com/JudgeOnline/problem.php?id=3771 题意:n个带价值互不相同的物品,每次可以取1.2.3个物品,问能得到的所有的价值和这个价值的方 ...
- virtual关键字的本质是什么?
MSDN上对virtual方法的解释:试着翻译如下 当一个方法声明包含virtual修饰符,这个方法就是虚方法.如果没有virtual修饰符,那么就不是虚方法. 非虚方法的实现是不变的:不管该方法是被 ...
- 500-internal server error 错误提示到配置文件的某行,并显示乱码
UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,又称万国码.UTF-8用1到4个字节编码UNICODE字符.用在网页上可 ...
- 五、点数器《苹果iOS实例编程入门教程》
该app为应用的功能为一个简单的数数程序 现版本 SDK 8.4 Xcode 运行Xcode 选择 Create a new Xcode project ->Single View Applic ...
- 基于 Node.js 平台,快速、开放、极简的 web 开发框架。
资料地址:http://www.expressjs.com.cn/ Express 基于 Node.js 平台,快速.开放.极简的 web 开发框架. $ npm install express -- ...
- [CareerCup] 15.3 Renting Apartment III 租房之三
Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...
- Centos 下安装 文泉驿 字体 Odoo
刚装完centos下的odoo的字体 文泉驿 ,一万头草泥马呼啸而过.....劝君如非必要,千万别再centos下折腾odoo..... 正题,文泉驿官网 只提供 deb包和源码包的字体安装 ,想在c ...
- android几个实用的判定代码
之前项目有几个判定代码很实用,特此做一个整理. 一.验证手机格式是否正确 //判断手机号码是否合理 private boolean judgePhoneNums(String phoneNums) { ...