【发布时间】:2015-07-22 16:46:55
【问题描述】:
我正在尝试了解有关 android 中复选框功能的更多信息。我想在 TextView 中显示选中复选框的值。在我的代码中,它显示了所有复选框,无论是 true(如果选中)还是 false(如果未选中),但我只想打印那些选中的复选框并排除那些未选中的复选框。我尝试使用“如果,否则如果”,但它不起作用。任何帮助将不胜感激。
主活动:
public class Demo extends Activity {
private CheckBox linux, macos, windows;
private Button button;
private EditText ed1, ed2;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
addListenerOnButton();
}
public void addListenerOnButton() {
linux = (CheckBox) findViewById(R.id.checkBox1);
macos = (CheckBox) findViewById(R.id.checkBox2);
windows = (CheckBox) findViewById(R.id.checkBox3);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.go);
text = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("Android check : ").append(linux.isChecked());
result.append("\nWindows OS check : ").append(macos.isChecked());
result.append("\niOS check :").append(windows.isChecked());
//Toast.makeText(Demo.this, result.toString(), Toast.LENGTH_LONG).show();
text.setText(result);
}
});
}
}
【问题讨论】: