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

单选钮在开发中提供了一种多选一的操作模式,也是经常见到的一种组件,例如:在选择文件编码的时候只能从多种编码中选择一种,或者是选择性别的时候只能从“男”或“女”之中选择一个,而在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复选框的使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值