Android中的RadioGroud与RadioButton(单选按钮)示例

目录

概述

一、环境

二、代码

三、运行结果

四、总结


概述

        项目需要,在此编写一篇关于单选按键的博文,分别有两种方法实现单选功能,废话少说,直接进入主题。

一、环境

开发环境(IDE):Android Studio 2021.1.1版本
运行环境:华为手机

二、代码

1)、activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:text="声音与振动" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="振动强度:" />

        <RadioGroup
            android:id="@+id/radiogroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:orientation="vertical">
            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="等级1" />

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="等级2" />

            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="等级3" />

        </RadioGroup>


        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="声音设置:" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:text="响铃" />

        <RadioButton
            android:id="@+id/radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:text="静音" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

2)、布局效果图

3)、MainActivity.java

package com.example.mytest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private RadioButton radioButton1,radioButton2,radioButton3;
    private Toast toast;

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

        init_View();
    }

    public void init_View(){

        /* 方法1:
        *   radioGroup
        * */
         radioGroup = (RadioGroup)findViewById(R.id.radiogroup1);
//         radioButton1 = (RadioButton)findViewById(R.id.radioButton1);
//         radioButton2 = (RadioButton)findViewById(R.id.radioButton2);
//         radioButton3 = (RadioButton)findViewById(R.id.radioButton3);

        View radioButton4 = findViewById(R.id.radioButton4);
        View radioButton5 = findViewById(R.id.radioButton5);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                View checkView = group.findViewById(checkedId);
                if (!checkView.isPressed()) {
                    return;
                }
                switch (checkedId) {
                    case R.id.radioButton1:
                        System.out.println("click radioButton1");
                        toast = Toast.makeText(getApplicationContext(),"等级1",Toast.LENGTH_SHORT);
//                        //toast.setGravity(Gravity.CENTER,0,0);
                        toast.show();
                        break;
                    case R.id.radioButton2:
                        System.out.println("click radioButton2");
                        toast = Toast.makeText(getApplicationContext(),"等级2",Toast.LENGTH_SHORT);
                        toast.show();
                        break;
                    case R.id.radioButton3:
                        System.out.println("click radioButton3");
                        toast = Toast.makeText(getApplicationContext(),"等级3",Toast.LENGTH_SHORT);
                        toast.show();
                        break;
                    default:
                        break;
                }
            }
        });

        /* 方法2:
         *   radioButton
         * */
        radioButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(radioButton4.isClickable()) {
                    mySetCheckedStateForView(R.id.radioButton5,false);
                    System.out.println("click radioButton4");
                    toast = Toast.makeText(getApplicationContext(),"响铃",Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        radioButton5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(radioButton5.isClickable()) {
                    mySetCheckedStateForView(R.id.radioButton4,false);
                    System.out.println("click radioButton5");
                    toast = Toast.makeText(getApplicationContext(),"静音",Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

    }

    private void mySetCheckedStateForView(int viewId, boolean checked) {
        View checkedView = findViewById(viewId);
        if (checkedView != null && checkedView instanceof RadioButton) {
            ((RadioButton) checkedView).setChecked(checked);
        }
    }

}

三、运行结果

四、总结

        好久没写Android,生疏不少,有空再继续更新Android的文章,谢谢各位看官赏阅^_^!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ch_champion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值