<RadioGroupandroid
android:id="@+id/gender"//設置id app
android:layout_width="fill_parent"ide
android:layout_height="wrap_content"佈局
android:orientation="vertical"//設置RadioGroup中RadioButton的排列方向 this
>spa
<RadioButtonxml
android:id="@+id/male"//設置id 對象
android:layout_width="fill_parent"blog
android:layout_height="wrap_content"事件
android:text="@string/male"
/>
<RadioButton
android:id="@+id/female"//設置id
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/female"
/>
</RadioGroup>
RadioGroup genderGroup = (RadioGroup)findViewById(R.id.gender);
RadioButton maleButton = (RadioButton)findViewById(R.id.male);
maleButton.setChecked(true);//默認選擇男
RadioButton femaleButton = (RadioButton)findViewById(R.id.female);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
publicvoid onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(femaleButton.getId()==checkedId){
Toast.makeText(ControlDemo2Activity.this, "Female", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(ControlDemo2Activity.this, "Male", Toast.LENGTH_SHORT).show();
}
}
});
<CheckBox
android:id="@+id/apple"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/apple"
/>
<CheckBox
android:id="@+id/orange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/orange"
/>
<CheckBox
android:id="@+id/mango"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mango"
/>
CheckBox appleCheck = (CheckBox)findViewById(R.id.apple);
appleCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
publicvoid onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==true)//當該Checkbox被按下
Toast.makeText(ControlDemo2Activity.this, "Apple Checked", Toast.LENGTH_SHORT).show();
else
Toast.makeText(ControlDemo2Activity.this, "Apple Unchecked", Toast.LENGTH_SHORT).show();
}
});
以上的兩種控件的樣式是能夠在佈局文件或者對象中能夠設置。
附件爲示例代碼,僅供參考。嘎嘎~
若是個人文章給與了你幫助,就不妨請我喝杯咖啡吧,點擊->