我的問題是,當單個複選框被選中時,它顯示它應該的文本。 但是,當兩個複選框都被選中時,它只顯示第二個文本。字符串返回數字而不是文本?
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