单选:
RadioButton:
单选按钮: 如果想要有互斥效果必须配合RadioGroup使用
是TextView的子类
属性:android:checked="" 设置默认选择 true 表示选中 false 表示 不选中
在代码中可通过实例化对象radioButton调用 setChecked方法达到同样的效果
RadioGroup:
单选按钮组:如果RadioButton 想要有互斥效果则必须配合RadioGroup使用
是LinearLayout的子类 横向或者纵向排列
代码:
<TextView android:layout_width="wrap_content" android:text="性别" android:layout_marginTop="5dp" android:textSize="20sp" android:layout_height="wrap_content" /><RadioGroup android :layout_width= "match_parent" android :orientation= "horizontal" android :layout_height= "wrap_content" > <RadioButton android :layout_width= "wrap_content" android :text= "男" android :layout_height= "wrap_content" /> <RadioButton android :layout_width= "wrap_content" android :text= "女" android :layout_height= "wrap_content" /> </RadioGroup>
效果图:
多选:
CheckBox:选择框
可以选中,再次点击则取消选中状态。
代码:
<TextView android:layout_width="wrap_content" android:text="爱好" android:layout_marginTop="5dp" android:textSize="20sp" android:layout_height="wrap_content" /> <CheckBox android:layout_width="wrap_content" android:text="篮球" android:layout_height="wrap_content" /> <CheckBox android:layout_width="wrap_content" android:text="乒乓球" android:layout_height="wrap_content" /> <CheckBox android:layout_width="wrap_content" android:text="羽毛球" android:layout_height="wrap_content" />
效果图: