5.单选钮:RadioButton
RadioGroup(单选组)中有若干个RadioButton
掌握RadioGroup类和RadioButton类的使用;
内容:
既然说到单选钮也同时回顾一下HTML中的内容:
<input type=“radio”>
单选钮在开发中提供可一种多选一的操作模式,也是经常见到的一种组件,例如:在选择文件编码的时候只能从多种编码中选择一种,或者是选择性别的时候只能从“男”或“女”之中选择一个,而在Android中可以使用radiogroup来定义单选钮组件,此类的定义如下:
Java.lang.Object
Android.view.View
Android.view.ViewGroup
Android.widget.LinearLayout
Android.widget.RadioGroup
LinearLayout属于布局管理器,所以对于单选钮而言也肯定需要采用一定的布局管理器。
对于很多的语言单选钮直接定义即可,但是在Android之中,RadioGroup定义的只是一个单选钮的容器,在这个容器之中以后要加入多个单选项,而这个单选项就是:RadioButton。
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
↳ android.widget.CompoundButton
↳ android.widget.RadioButton
RadioButton是Button的子类,但是同时也是CompoundButton子类
<TextView android:id="@+id/encinfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20px" 文字大小像素20 android:text="请选择要使用的文字编码:" 默认文字 /> |
之所以这样定义,主要的目的是加入一些提示的说明文字。
<RadioGroup android:id="@+id/encoding" 组件的应用ID,以后程序中可以进行控制 android:layout_width="fill_parent" 组件的宽度是屏幕宽度 android:layout_height="wrap_content" 组件的高度由所包含的内容决定 android:orientation="vertical" 所有的列表项都通过垂直排列 android:checkedButton="@+id/gbk" 表示默认选项选中的RadioButton组件 > <RadioButton 定义列表项 android:id="@+id/utf" 组件的ID android:text="UTF编码" 显示文字 android:layout_height="wrap_content"组件的高度由所包含的内容决定 android:layout_width="fill_parent"/> 组件的宽度是屏幕宽度 <RadioButton android:id="@+id/gbk" android:text="GBK编码" android:layout_height="wrap_content" android:layout_width="fill_parent"/> </RadioGroup> |
以上的显示是采用了一垂直的方式进行了内容的显示,那么下面将其使用水平的方式进行摆放:
<TextView android:id="@+id/sexinfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20px" android:text="您的性别是:" /> <RadioGroup 定义单选组 android:id="@+id/sex" 组件ID android:layout_width="fill_parent" 宽度为屏幕宽度 android:layout_height="wrap_content" 高度为所有选项的高度 android:orientation="horizontal" 里面的所有组件都按照水平的方式进行排列 android:checkedButton="@+id/male" 默认的选中项 > 这个尖括号没有/ <RadioButton 定义选项钮 android:id="@+id/male" 表示组件的ID android:text="男" 表示组件的名称 android:layout_height="wrap_content" android:layout_width="fill_parent"/> <RadioButton android:id="@+id/female" android:text="女" android:layout_height="wrap_content" android:layout_width="fill_parent"/> </RadioGroup> |
准备出了第二个单选钮,唯一的不同的在于现在的组件采用的水平的方式进行排列。
此时的目的也只是进行了一个选项的显示,但是如果要想让这些选项变得更加有意义一些,也需要日后结合各个事件处理操作完成。
总结:
掌握radioGroup类相当于定义了一个单选钮的容器;
RadioButton类用于定义单选钮中的内容。