RadioButton控件的使用方式

RadioButton控件的使用方式

RadioButton表示单选按钮,它是Button的子类。每一个单选按钮都有“选中”和“未选中”两种状态,这两种状态是通过android:checked属性指定的。当可选值为ture,表示选中状态,否则,表示未选中状态。
在Android程序中RadioButton通常是和RadioGroup配合使用,实现RadioButton的单选功能。RadioGroup是单选组合框,可以容纳多个RadioButton,但是在RadioGroup中不会出现多个RadioGroup同时选中的情况。

RadioGruop设置xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
        android:id="@+id/rbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="男"/>
        <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="女"/>
    </RadioGroup>
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"/>
</LinearLayout>

上诉代码中,第6~22行代码定义了RadioGroup布局,第10行代码通过设置android:onrientation属性的值为“vertical”,实现RadioGroup布局中的控件竖直排列。
第11~22行代码定义了2个RadioButton控件,这两个没有设置android:check属性的值,默认情况下该属性的值为false,因此界面上两个单选按钮为未选中状态

RadioGruop设置监听事件

代码如下(示例):

	package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rdg);
        textView = (TextView) findViewById(R.id.tv);
        //利用setOnCheckedChangeListener()为RadioGroup设置监听事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
                    public void onCheckedChanged(RadioGroup group,int checkedId){
                if(checkedId == R.id.rbtn){
                    textView.setText("您的性别是:男");
                }
                else{
                    textView.setText("您的性别是:女");
                }
            }
        });
    }
}

上述代码,第16~27行代码通过setOnCheckedChangeListener()方法为RadioGroup设置监听布局内空间状态是否有改变的时间,通过事件返回的onCheckedChanged()方法获取被点击的控件ID,在TextView中显示相应的信息。

  • 4
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值