【安卓学习笔记】Android Studio第5课——RadioButton

RadioButton就是常用到的单选按钮,一般常用到RadioGroup,一个RadioGroup里面可以有多个RadioButton,这里面的单选按钮一次只能选一个,比如常见的性别的选项,只能选择male或者female。一个Activity里面当然也可以有多个RadioGroup,不同的RadioGroup互相不影响,这里就简单的测试了如下图:


功能也很简单,这里面放置了两个RadioGroup,每个组里面有两个RadioButton,在代码上就简单的写了下测试函数。设置对应两个RadioButton的CheckedChange监听事件, 然后在方法体里面添加同步显示,方法很简单,比如选中了“男”就把对应的“A”选中,选中了“女”就把对应的“B”给选中,那么反过来也是同样的。贴上代码:

package com.example.urien.secondapp;

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

public class Lesson5_Activity extends AppCompatActivity {

    //1.声明控件
    private RadioGroup L5_radioGroup;
    private RadioButton L5_radioButtonFemale;
    private RadioButton L5_radioButtonMale;

    private RadioGroup L5_radioGroup2;
    private RadioButton L5_radioButtonA;
    private RadioButton L5_radioButtonB;

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

        //2.找到控件
        L5_radioGroup = findViewById(R.id.L5_radioGroup);
        L5_radioButtonFemale = findViewById(R.id.L5_radio_female);
        L5_radioButtonMale = findViewById(R.id.L5_radio_male);

        L5_radioGroup2 = findViewById(R.id.L5_radioGroup2);
        L5_radioButtonA = findViewById(R.id.L5_radio_A);
        L5_radioButtonB = findViewById(R.id.L5_radio_B);

        //4.设置监听器
        L5_radioGroup.setOnCheckedChangeListener(new radioGroupListener());
        L5_radioGroup2.setOnCheckedChangeListener(new radioGroupListener());

        L5_radioButtonMale.setOnCheckedChangeListener(new radioButtonListener());
    }


    //3.实现监听器接口方法
    class radioButtonListener implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            System.out.println("male is selected ————>" + isChecked);
        }
    }
    class radioGroupListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(group.getId() == L5_radioGroup.getId()){
                if(checkedId == R.id.L5_radio_male){
                    System.out.println("选中了male");
                    L5_radioButtonA.setChecked(true);
                }
                else if(checkedId == R.id.L5_radio_female){
                    System.out.println("选中了female");
                    L5_radioButtonB.setChecked(true);
                }
            }
            else if(group.getId() == L5_radioGroup2.getId()){
                if(checkedId == L5_radioButtonA.getId()){
                    System.out.println("选中了A");
                    L5_radioButtonMale.setChecked(true);
                }
                else if(checkedId == L5_radioButtonB.getId()){
                    System.out.println("选中了B");
                    L5_radioButtonFemale.setChecked(true);
                }
            }
        }
    }
}
By Urien 2018年5月12日 21:50:47


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,RadioButton和RadioGroup是常用的单选控件。RadioButton用于单个选项,而RadioGroup用于将多个RadioButton组合在一起,以便用户可以从中选择一个选项。 以下是一个使用RadioGroup和RadioButton的示例: 1. 在XML布局文件中添加RadioGroup和RadioButton: ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> </RadioGroup> ``` 2. 在Java代码中获取RadioGroup和RadioButton,并设置选中的选项: ```java RadioGroup radioGroup = findViewById(R.id.radioGroup); RadioButton radioButton1 = findViewById(R.id.radioButton1); RadioButton radioButton2 = findViewById(R.id.radioButton2); RadioButton radioButton3 = findViewById(R.id.radioButton3); // 设置默认选中的选项 radioButton1.setChecked(true); // 监听选项变化 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radioButton1: // 选中了Option 1 break; case R.id.radioButton2: // 选中了Option 2 break; case R.id.radioButton3: // 选中了Option 3 break; } } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值