Beginning Android 4 中 Demo Basic/Switch 的问题.
2016-01-08 15:34
483 查看
作者的版本:
layout(main.xml):
java代码:
作者提供的java代码有问题.原因参考(http://stackoverflow.com/questions/29811646/android-settexton-not-working-in-oncheckchanged-method)
Idon'tthinkyourcallstosetTextOnandsetTextOffneedtobeinanif-theyjustdefinehowthetoggleappearswhenonoroff,sotheydon'tneedtobesetconditionally.Ref:API–SimonMᶜKenzieApr23'15at1:01
The
Thetext"Theswitchis:On"isjustthelabelofyourSwitchanddoesnotconveythestateofyour
Toachievetheresultthatyouwant,youneedtocall
oryoucanadditinyourXML.
Asobservedby@SimonM,thisxmlandjavasnippetproduceconsistentoutputasshownbythescreenbelow.
layout(main.xml):
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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代码:
publicclassSwitchActivityextendsActivity implementsCompoundButton.OnCheckedChangeListener{ Switchsw; @Override publicvoidonCreate(Bundleicicle){ super.onCreate(icicle); setContentView(R.layout.main); sw=(Switch)findViewById(R.id.switchdemo); sw.setOnCheckedChangeListener(this); } //@Override publicvoidonCheckedChanged(CompoundButtonbuttonView, booleanisChecked){ if(isChecked){ sw.setTextOn("Thisswitchis:on"); } else{ sw.setTextOff("Thisswitchis:off"); } } }
作者提供的java代码有问题.原因参考(http://stackoverflow.com/questions/29811646/android-settexton-not-working-in-oncheckchanged-method)
Idon'tthinkyourcallstosetTextOnandsetTextOffneedtobeinanif-theyjustdefinehowthetoggleappearswhenonoroff,sotheydon'tneedtobesetconditionally.Ref:API–SimonMᶜKenzieApr23'15at1:01
The
setTextOnand
setTextOfffunctionsaretousedtosetthelabelsdependingonthestateofthe
Switch.
Thetext"Theswitchis:On"isjustthelabelofyourSwitchanddoesnotconveythestateofyour
Switch.
Toachievetheresultthatyouwant,youneedtocall
setShowText(true):
sw=(Switch)findViewById(R.id.swish); sw.setShowText(true);
oryoucanadditinyourXML.
<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"/>
Asobservedby@SimonM,thisxmlandjavasnippetproduceconsistentoutputasshownbythescreenbelow.
<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(newCompoundButton.OnCheckedChangeListener(){ @Override publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){ //absolutelynothing });
答案说的很清楚了.只需要在onCreate里面调用setTextOn/Off即可.onCheckedChanged什么都不用做.或者根本步调用setTextOn/Off,直接在main.xml中用`android::textOn/Off`即可.
下面是修改后的java实现,没有修改main.xml,也展示了setText与setTextOn/Off的区别.
publicclassSwitchActivityextendsActionBarActivityimplementsCompoundButton.OnCheckedChangeListener{
Switchsw;
@Override
publicvoidonCreate(Bundleicicle){
super.onCreate(icicle);
setContentView(R.layout.main);
sw=(Switch)findViewById(R.id.switchdemo);
sw.setOnCheckedChangeListener(this);
sw.setTextOff("#OFF#");
sw.setTextOn("#ON#");
}
publicvoidonCheckedChanged(CompoundButtonbuttonView,
booleanisChecked){
if(isChecked){
sw.setText("I'mOn.");
}else{
sw.setText("I'mOff.");
}
}
}
相关文章推荐
- Android样式开发系列文章
- Android 仿今日头条频道管理(上)(GridView之间Item的移动和拖拽)
- Android平台开发之线程和进程的详解
- Parcelable和Serializable
- Android实现点击Button产生水波纹效果
- 简单说说Android自定义view学习推荐的方式
- 简单说说Android自定义view学习推荐的方式
- 【android_温故知新】android 应用的资源
- Android Trace View使用
- Android取消EditText自动获取焦点默认行为
- 访问 Android Developers 403 错误
- android 新闻demo (暂时有一个bug)
- Android4.0窗口机制token分析以及activitiy, dialog, toast 窗口创建过程分析
- Android Fragment详解(二):fragment创建及其生命周期
- android ListView多线程解决图片错位问题、缩略图问题、实现顺滑滚动
- android 开发安全简单想法
- Android中SharedPreference使用实例讲解
- Android单元测试研究与实践
- Android 触摸及手势操作GestureDetector
- Android版本和API Level对应关系及输出本机的版本信息