[Android]RadioButton控件

 RadioButton控件


RadioButton控件是单选按钮控件,它继承自Button控件,可以直接使用Button控件支持的各种属性和方法。

与普通按钮不同的是,RadioButton控件多了一个可以选中的功能,能额外指定一个android:checked属性,该属性可以指定初始状态是否被选中。其实也可以不用指定,默认初始状态都不被选中。

RadioButton控件必须和单选框RedioGroup控件一起使用。

 RadioButton控件的使用


<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一年级"
            android:checked="true"
            />
        <RadioButton
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="二年级"
            />
        <RadioButton
            android:id="@+id/three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="三年级"
            />
    </RadioGroup>

  RadioButton控件的选中监听器


//为RadioGroup设置一个事件监听器
        val radioGroup:RadioGroup=findViewById(R.id.radiogroup)
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            val check:RadioButton=findViewById(checkedId)
            Toast.makeText(this,"${check.text}",Toast.LENGTH_SHORT).show()
        }

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RadioButton件是一种单选按钮,它通常用于在多个选项中选择一个选项。在Android中,使用RadioButton件可以轻松地创建一个单选按钮列表。 以下是RadioButton件的使用方法: 1. 在XML布局文件中添加RadioButton件。 ``` <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 1"/> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton 2"/> ``` 2. 在Java代码中获取RadioButton件的引用。 ``` RadioButton radioButton1 = findViewById(R.id.radio_button1); RadioButton radioButton2 = findViewById(R.id.radio_button2); ``` 3. 设置RadioButton件的监听器,以便在用户单击件时处理事件。 ``` radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // 处理单选按钮被选中的情况 } } }); radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // 处理单选按钮被选中的情况 } } }); ``` 4. 在处理事件的方法中,可以根据RadioButton件的状态来执行不同的操作。 ``` if (radioButton1.isChecked()) { // RadioButton 1被选中,执行相应的操作 } else if (radioButton2.isChecked()) { // RadioButton 2被选中,执行相应的操作 } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值