我的问题是,当单个复选框被选中时,它显示它应该的文本。 但是,当两个复选框都被选中时,它只显示第二个文本。字符串返回数字而不是文本?
public class MainActivity extends Activity {
TextView text;
CheckBox firstCheck, secondCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstCheck = (CheckBox) findViewById(R.id.checkBox1);
secondCheck = (CheckBox) findViewById(R.id.checkBox2);
text = (TextView) findViewById(R.id.textView2);
}
public void buttonClick(View view) {
if(firstCheck.isChecked()) {
text.setText(R.string.checkbox_1);
}
if(secondCheck.isChecked()) {
text.setText(R.string.checkbox_2);
}
if(firstCheck.isChecked()==false && secondCheck.isChecked()==false) {
text.setText(R.string.unchecked);
}
}
}
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Les 2_1</string>
<string name="hello_world">Hello world!</string>
<string name="checkbox_1">Checkbox 1 </string>
<string name="checkbox_2">Checkbox 2</string>
<string name="button_1">Click here!</string>
<string name="textview_2">And?</string>
<string name="unchecked">No buttons checked!</string>
</resources>
很好地解决这个它去,你当它通过'secondCheck' – tyczj 2014-11-03 16:40:38
还覆盖文本firstCheck.isChecked()== false是多余的,使用!firstCheck.isChecked() – reactivemobile 2014-11-03 16:41:56