Android学习之CheckBox控件

public class MainActivity extends ActionBarActivity implements
OnCheckedChangeListener {

// 实例控件
private TextView textView;
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 初始化控件
textView = (TextView) findViewById(R.id.textView2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);

// 复选框添加oncheckedChangeListen监听
checkBox1.setOnCheckedChangeListener(this);
checkBox2.setOnCheckedChangeListener(this);
checkBox3.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.checkBox1:
if (isChecked) {
textView.setText(textView.getText()
+ checkBox1.getText().toString());
}
break;
case R.id.checkBox2:
if (isChecked) {
textView.setText(textView.getText()
+ checkBox2.getText().toString());
}
break;
case R.id.checkBox3:
if (isChecked) {
textView.setText(textView.getText()
+ checkBox3.getText().toString());
}
break;
default:
break;
}
}
}

你可能感兴趣的:(Android学习)