Android 字符串返回数字而不是文本?
我的问题是,当选中一个复选框时,它会显示它应该显示的文本。 但是当两个复选框都选中时,它只显示第二个复选框的文本Android 字符串返回数字而不是文本?,android,Android,我的问题是,当选中一个复选框时,它会显示它应该显示的文本。 但是当两个复选框都选中时,它只显示第二个复选框的文本 public class MainActivity extends Activity { TextView text; CheckBox firstCheck, secondCheck; @Override protected void onCreate(Bundle savedInstanceState) { super.onCr
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>
莱斯2_1
你好,世界!
复选框1
复选框2
点击这里!
然后呢?
没有按钮检查!
您的代码
if(secondCheck.isChecked()) {
text.setText(R.string.checkbox_2);
}
正在覆盖文本,因此您需要附加复选框_2文本,而不仅仅是设置它
if(firstCheck.isChecked()) {
text.setText(R.string.checkbox_1);
}
if(secondCheck.isChecked()) {
text.setText(R.string.checkbox_2);
}
如果同时输入了If,则第二个setText将覆盖第一个setText的文本
您可以通过追加文本来解决此问题
Resources res = getResources();
text.setText(text.getText().toString() + res.getString(R.string.checkbox_x));
或者添加一个if,以涵盖这两个方面
if (firstCheck.isChecked() && secondCheck.isChecked()) {
text.setText(res.getString(R.string.checkbox_1) + res.getString(R.string.checkbox_2));
}
当然,当文本通过
secondCheck
和firstCheck时,您会覆盖文本。isChecked()==false是多余的,请使用!firstCheck.isChecked()text.setText(R.string.checkbox1+R.string.checkbox2);无法工作,因为R.string.checkbox1和2返回int值!因此,您访问了错误的资源(如果存在的话)@haywire我刚才集中讨论了问题中的问题。。无法检查所有内容;-)可以,但请更正它,因为它不起作用。由于冲突,您的编辑被拒绝,您可以现在重试