您的位置:首页 > 移动开发 > Android开发

从零开始学android<RadioButton单选按钮的使用.七.>

2014-04-06 19:40 525 查看
单选钮在开发中提供了一种多选一的操作模式,也是经常见到的一种组件,例如:在选择文件编码的时候只能从多种编码中选择一种,或者是选择性别的时候只能从“男”或“女”之中选择一个,而在Android中可以使用RadioGroup来定义单选钮组件,此类的定义如下:
java.lang.Object
↳android.view.View
↳android.view.ViewGroup
↳android.widget.LinearLayout
↳android.widget.RadioGroup

Radiogroup常用的方法如下

No.
方法
类型
描述
1
public void check (int id)
普通
设置要选中的单选钮编号
2
public void clearCheck ()
普通
清空选中状态
3
public int getCheckedRadioButtonId()
普通
取得选种按钮的RadioButton的ID
4
public void setOnCheckedChangeListener(
RadioGroup.OnCheckedChangeListener listener)
普通
设置单选钮选中的操作事件
RadioGroup提供的只是一个单选钮的容器,只有在此容器之中配置多个按钮组件之后才可以使用,而要想设置单选钮的内容,则需要使用RadioButton类,此类定义如下:
java.lang.Object
↳android.view.View
↳android.widget.TextView
↳android.widget.Button
↳android.widget.CompoundButton
↳android.widget.RadioButton

…………………………………………………………毫无美感的分割线…………………………………………………………

用一个例子简单说明
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView //提示框
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择您的性别"
/>
<RadioGroup//包裹RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/male"
>

<RadioButton //第一个选项
android:id="@+id/male"
android:text="男"/>
<RadioButton //第二个选项
android:id="@+id/fmale"
android:text="女"/>

</RadioGroup>
</LinearLayout>
JAVA文件不用进行配置。



当然,大家也可以配置呢多个RadioButton的选项,也可以配置第一个RadioGroup。大家下面可以自己试试
下节预报:CheckBox复选框的使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: