您的位置:首页 > 移动开发 > Android开发

Beginning Android 4 中 Demo Basic/Switch 的问题.

2016-01-08 15:34 483 查看
作者的版本:

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
setTextOn
and
setTextOff
functionsaretousedtosetthelabelsdependingonthestateofthe
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.");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
章节导航