CheckBox和RadioButton都是Button的子类。CheckBox是多选框,只有选中和未选中两种状态。RadioButton是单选按钮,需要使用一个RadioGroup来组织多个或一个RadioButton,在同一个RadioGroup中,一次只能选中一个RadioButton。多选和单选的区别和用处就不需要我再多说了吧,下面直接对其属性进行说明。
CheckBox
CheckBox在布局xml文件中的创建(一般都会创建多个):
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="20sp"
android:gravity="center"/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读书"
/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打篮球"/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑步"/>
<Button
android:id="@+id/my_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:onClick="onClick"
android:background="@drawable/shape" />
在类中进行CheckBox控件的定义以及监听事件:
mButton = (Button) findViewById(R.id.my_button);
//Button的监听器
mButton.setOnClickListener(this);
//获取提交按钮
mCheckBox1 = (CheckBox) findViewById(R.id.checkbox1);
mCheckBox2 = (CheckBox) findViewById(R.id.checkbox2);
mCheckBox3 = (CheckBox) findViewById(R.id.checkbox3);
//设置CheckBox的监听器
mCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mCheckBox1.isChecked()){
mCheckBox1.getText();
}
}
});
mCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mCheckBox2.isChecked()){
mCheckBox2.getText();
}
}
});
mCheckBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mCheckBox3.isChecked()){
mCheckBox3.getText();
}
}
});
Button的点击事件:
@Override
public void onClick(View v) {
String checkBox = "爱好是:";//保存选中的值
if (mCheckBox1.isChecked()) //表示被选中的情况
checkBox+=mCheckBox1.getText().toString()+" ";
if (mCheckBox2.isChecked())
checkBox+=mCheckBox2.getText().toString()+" ";
if (mCheckBox3.isChecked())
checkBox+=mCheckBox3.getText().toString()+" ";
Toast.makeText(this,checkBox,Toast.LENGTH_SHORT).show();
}
效果图如下:
RadioButton&RaidoGroup
RadioButton控件为用户提供由两个或多个互斥选项组成的选项集,需要使用一个RadioGroup来组织多个RadioButton,只能选择一个,比如在选择性别的时候只能有一个选项,这里就用到的是RadioButton。
RadioButton在布局的xml文件中代码如下:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="20sp"/>
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/><!--设置默认选项-->
<RadioButton
android:id="@+id/radio_fmale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
效果如下:
其点击事件属性和CheckBox大同小异,在此不做赘述。