在定义单选按钮的时候RadioGroup包含若干颗RadioButton;
目标:掌握RadioGroup类和RadioButton类的使用
RadioGroup 类继承结构: java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.LinearLayout | |||
↳ | android.widget.RadioGroup |
LinearLayout属于线性布局,所以对于单选按钮也肯定需要采用一定的布局管理
对于很多语言单选按钮直接定义即可,但在android中RadioGroup定义的知识一个单选按钮的容器,在容器之中要加入多个单选项,这个单选项就是RadioButton
RadioButton类继承结构:
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.widget.TextView | ||||
↳ | android.widget.Button | ||||
↳ | android.widget.CompoundButton | ||||
↳ | android.widget.RadioButton |
RadioButton是Button的子类,同时也是CompoundButton的子类
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/encinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:text="请选择要使用的文字编码:" />
<RadioGroup -->定义选项组
android:id="@+id/encoding"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"> --->水平显示
<RadioButton -->定义选项
android:id="@+id/utf"
android:checked="true" ---->默认选中
android:text="utf编码"/>
<RadioButton -->定义选项
android:id="@+id/gbk"
android:text="gbk编码"/>
</RadioGroup>
</LinearLayout>
总结:单选钮必须放在RadioGroup中,而每个RadioButton表示单选钮中的内容。每个RadioGroup中包含多个RadioButton.