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

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

【安卓学习笔记】Android Studio第5课——RadioButton_第1张图片

功能也很简单,这里面放置了两个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


你可能感兴趣的:(安卓)