Android开发-Android常用组件-RadioButton单选按钮

4.5  RadioButton(单选按钮)

  1. RadioButton (单选按钮) 基本用法与事件处理:

    如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现单选功能。先熟悉下如何使用RadioButton,一个简单的性别选择的例子:另外我们可以为外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平。

<?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:id="@+id/linerLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别:"
        android:textSize="23sp"/>
    
    <RadioGroup
        android:id="@+id/group1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/buttonMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/buttonWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
        
    </RadioGroup>
    <Button
        android:id="@+id/btnpost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>
    
</LinearLayout>

  • 获得选中的值:这里有两种方法

  第一种是为RadioButton 设置一个事件监听器setOnCheckChangeListener

   在java的application包中新建一个RadioButtonActivity.java。

 

package com.example.myapplication;

import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RadioButtonActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio_button);
        Button btnchage = (Button) findViewById(R.id.btnpost);
        final RadioGroup radgroup = (RadioGroup) findViewById(R.id.group1);
        //第一种是为RadioButton 设置一个事件监听器setOnCheckChangeListener
        radgroup.setOnCheckedChangeListener(((group, checkedId) -> {
            RadioButton radioButton = (RadioButton) findViewById(checkedId);
            Toast.makeText(getApplicationContext(),"你选了:"+radioButton.getText(),Toast.LENGTH_LONG).show();
        }));
       
    }
}

PS:另外有一点要切记,要为每个 RadioButton 添加一个id,不然单选功能不会生效!!!

  第二种方法是通过单击其他按钮获取选中单选按钮的值,当然我们也可以直接获取,这个看需求。

package com.example.myapplication;

import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RadioButtonActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio_button);
        Button btnchage = (Button) findViewById(R.id.btnpost);
        final RadioGroup radgroup = (RadioGroup) findViewById(R.id.group1);
        //第一种是为RadioButton 设置一个事件监听器setOnCheckChangeListener
        radgroup.setOnCheckedChangeListener(((group, checkedId) -> {
            RadioButton radioButton = (RadioButton) findViewById(checkedId);
            Toast.makeText(getApplicationContext(),"你选了:"+radioButton.getText(),Toast.LENGTH_LONG).show();
        }));
        //第二种方法是通过单击其他按钮获取选中单选按钮的值,当然我们也可以直接获取,这个看需求。
        btnchage.setOnClickListener((v -> {
            for (int i = 0; i < radgroup.getChildCount(); i++) {
                RadioButton rb = (RadioButton) radgroup.getChildAt(i);
                if (rb.isChecked()){
                    Toast.makeText(getApplicationContext(),"你选择的是:"+rb.getText(),Toast.LENGTH_LONG).show();
                    break;
                }
            }
        }));

    }
}

代码解析: 这里我们为提交按钮设置了一个setOnClickListener 事件监听器,每次点击的话遍历一次RadioGroup判断哪个按钮被选中我们可以通过下述方法获得RadioButton的相关信息。

  • getChildCount( )获得按钮组中的单选按钮的数目;
  • getChinldAt(i):根据索引值获取我们的单选按钮;
  • isChecked( ):判断按钮是否选中。

 测试:将main包下的AndroidMainifest.xml文件中的<application>标签下<activity>标签中的android:name的值改为.RadioButtonActivity

android:name=".RadioButtonActivity"

 启动测试。

当切换选择内容时:

 当提交时:

猜你喜欢

转载自blog.csdn.net/LYly_B/article/details/129849341