Android RadioButton单选框的使用方法

时间:2021-10-02 14:37:20 


public class MainActivity extends Activity {

 public RadioGroup mRadioGroup1;
 public RadioButton mRadio1, mRadio2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mRadioGroup1 = (RadioGroup) findViewById(R.id.gendergroup);
  mRadio1 = (RadioButton) findViewById(R.id.girl);
  mRadio2 = (RadioButton) findViewById(R.id.boy);
  mRadioGroup1.setOnCheckedChangeListener(radiogpchange);

 }

 private RadioGroup.OnCheckedChangeListener radiogpchange = new RadioGroup.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
   if (checkedId == mRadio1.getId()) {
    Toast.makeText(getApplicationContext(), "女孩", 1).show();
   } else if (checkedId == mRadio2.getId()) {
    Toast.makeText(getApplicationContext(), "男孩", 1).show();
   }
  }
 };
}

RadioButton:就像是C#中的Radio控件,可以为控件设置Group,每个Group中的项只能选择一项;


    <RadioGroup

        android:id="@+id/gendergroup"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical" >

        <RadioButton

            android:id="@+id/girl"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/girl" />

        <RadioButton

            android:id="@+id/boy"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/boy" />

    </RadioGroup>

标签:android,单选框
0
投稿

猜你喜欢

  • Java状态设计模式实现对象状态转换的优雅方式

    2023-11-20 10:33:59
  • 解决springboot环境切换失效的问题

    2023-11-11 20:40:35
  • 浅谈Android开发中项目的文件结构及规范化部署建议

    2022-05-13 12:47:37
  • SpringBoot之bootstrap和application的区别解读

    2022-05-03 20:23:07
  • 浅谈Maven的安装及修改为阿里云下载依赖

    2023-08-05 08:30:37
  • java开发之读写txt文件操作的实现

    2023-11-17 06:00:23
  • 如何在Springboot实现拦截器功能

    2023-01-10 01:11:38
  • Android RecyclerView添加FootView和HeadView

    2022-12-11 21:17:11
  • C#文件断点续传实现方法

    2023-09-07 18:35:05
  • Spring Boot项目@RestController使用重定向redirect方式

    2023-12-11 15:21:56
  • Java 实战项目锤炼之IT设备固定资产管理系统的实现流程

    2023-02-06 01:31:39
  • android 实现按钮浮动在键盘上方的实例代码

    2022-12-21 11:32:33
  • android 调用系统的照相机和图库实例详解

    2022-01-02 11:46:47
  • SpringBoot数据访问自定义使用Druid数据源的方法

    2023-01-10 16:29:38
  • 利用ssh实现服务器文件上传下载

    2021-10-16 09:28:47
  • Spring4如何自定义@Value功能详解

    2021-12-16 06:31:12
  • C#实现简单的Login窗口实例

    2021-07-18 02:23:29
  • java基础之字符串编码知识点总结

    2023-06-20 19:05:09
  • C++异步操作future和aysnc与function和bind

    2023-06-21 06:48:50
  • 使用位运算实现网页中的过滤、筛选功能实例

    2023-06-04 10:00:31
  • asp之家 软件编程 m.aspxhome.com