
CompoundButton在XML文件中主要使用下面两个属性。
CompoundButton在java代码中主要使用下列4种方法。
- <CheckBox
- android:id="@+id/ck_system"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="5dp"
- android:text="系统CheckBox"/>

Switch是开关按钮,它在选中与取消选中时可展现的界面元素比复选框丰富。
Switch控件新添加的XML属性说明如下:
- <Switch
- android:id="@+id/sw_status"
- android:layout_width="80dp"
- android:layout_height="30dp"
- android:padding="5dp"/>

单选按钮要在一组按钮种选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组RadioGroup。
RadioGroup实际上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下,除了RadioButton,也允许放置其他控件。
判断选中了哪个单选按钮,通常不是监听某个单选按钮,而是监听单选组的选中事件。
RadioGroup常用的3个方法:
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="请选择性别"/>
- <RadioGroup
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <RadioButton
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="男"/>
- <RadioButton
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="女"/>
- </RadioGroup>
