Android控件CheckBox和RadioButton

以下为RadioGroup:(多个单选)

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioButton0 = (RadioButton) findViewById(R.id.radio0);
radioButton1 = (RadioButton) findViewById(R.id.radio1);
radioButton2 = (RadioButton) findViewById(R.id.radio2);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == radioButton0.getId()){
//这里处理radioButton0按钮。
Toast.makeText(MainActivity.this, "按钮0", Toast.LENGTH_LONG).show();
}
if(checkedId == radioButton1.getId()){
//这里处理radioButton1按钮。
Toast.makeText(MainActivity.this, "按钮1", Toast.LENGTH_LONG).show();
}
if(checkedId == radioButton2.getId()){
//这里处理radioButton2按钮。
Toast.makeText(MainActivity.this, "按钮2", Toast.LENGTH_LONG).show();
}
}


});

}

以下为CheckBox:(单选)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox = (CheckBox) findViewById(R.id.checkBox1);


checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Toast.makeText(MainActivity.this, isChecked ? "选中" : "未选中",Toast.LENGTH_LONG).show();//被选中isChecked则为true。
}
});
}

你可能感兴趣的:(checkbox,button,Radio)