zoukankan      html  css  js  c++  java
  • 简述RadioGroup和RadioButton的使用

    简述RadioGroup和RadioButton的使用

    在项目中有身份选择的选项,需要用到RadioGroup和RadioButton,这里做个记录,和大家交流一下。

    话不多说,一起看代码

    XML代码

    <RadioGroup
                android:id="@+id/login_radiogroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="horizontal">
    
                <RadioButton
                    android:id="@+id/admin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="@string/admin"/>
    
                <RadioButton
                    android:id="@+id/tech"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="@string/tech"/>
    
                <RadioButton
                    android:id="@+id/market"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="@string/market"/>
    
                <RadioButton
                    android:id="@+id/guest"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="@string/guest"/>
    
            </RadioGroup>

    这里使用了layout_gravity让RadioGroup居中显示,使用了orientation属性,horizontal让内部的RadioButton呈水平摆放,vertical就是垂直摆放。

    JAVA代码

    这里因为在Fragment里面实例化布局,所有用了view.findViewById(),在Activity中的话去掉view就可以。

    private RadioGroup mRadioGroup;
    mRadioGroup=(RadioGroup)view.findViewById(R.id.login_radiogroup);

    接下来在活动中实现对RadioGroup的监听。

     1 mRadioGroup.setOnCheckedChangeListener(new CheckListener());
     2 
     3 class CheckListener implements RadioGroup.OnCheckedChangeListener{
     4 
     5         @Override
     6         public void onCheckedChanged(RadioGroup group, int checkedId) {
     7             switch (checkedId){
     8                 case R.id.admin:
     9                     //执行具体操作
    10                     break;
    11 
    12                 case R.id.tech:
    13                     //执行具体操作
    14                     break;
    15 
    16                 case R.id.market:
    17                     //执行具体操作
    18                     break;
    19 
    20                 case R.id.guest:
    21                     //执行具体操作
    22                     break;
    23 
    24                 default:
    25                     break;
    26 
    27             }
    28         }
    29     }

    是个菜鸟,有错误还希望大家能指出来。

    欢迎大家有好的想法一起交流。

  • 相关阅读:
    Linux IO接口 监控 (iostat)
    linux 防火墙 命令
    _CommandPtr 添加参数 0xC0000005: Access violation writing location 0xcccccccc 错误
    Visual Studio自动关闭
    Linux vsftpd 安装 配置
    linux 挂载外部存储设备 (mount)
    myeclipse 9.0 激活 for win7 redhat mac 亲测
    英文操作系统 Myeclipse Console 乱码问题
    Linux 基本操作命令
    linux 查看系统相关 命令
  • 原文地址:https://www.cnblogs.com/hzauxx/p/11002763.html
Copyright © 2011-2022 走看看