Android radio button

Radio Buttons
By default, radio buttons appear as a round icon, and you can put some descriptive text next to the icon. Normally the RadioButton component needs to be used together with the RadioGroup component to form a radio button group. RadioGroup is a container that can hold multiple RadioButtons.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="选择性别:"
        android:textSize="25sp"
        android:gravity="center"
        android:textColor="@color/black"/>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center">
        <RadioButton
            android:id="@+id/radio_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textColor="@color/black"
            android:textSize="25sp"
            android:checked="true"/>

        <RadioButton
            android:id="@+id/radio_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textColor="@color/black"
            android:layout_marginLeft="100dp"
            android:textSize="25sp"/>
    </RadioGroup>

</LinearLayout>

<Button
    android:id="@+id/bt_submit"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:text="提交"
    android:textSize="20sp"
    android:layout_marginTop="10dp"
    android:layout_gravity="center"/>

The layout effect shows:
Insert image description here

The android:checked attribute of the RadioButton component is used to specify the selected state. When android:checked="true", it means selected; when android:checked="false", it means unchecked.
There are three ways to get the selected value:
The first is to set an event listener setOnCheckChangeListener for RadioButton.

public class MainActivity extends AppCompatActivity {
    
    

    RadioGroup radioGroup;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //点击事件
        clickEvent();
    }

    private void initView() {
    
    
        radioGroup = findViewById(R.id.radioGroup);
    }

    private void clickEvent() {
    
    
        //给RadioGroup绑定监视器
        radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
    }

    //单选按钮监听
    private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
    
    
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    

            RadioButton r = (RadioButton) findViewById(checkedId);//获取被选中的Id
            Log.i("单选按钮监听", "选择性别为:" + r.getText().toString());
        }
    }
}

Radio button listening log effect:
Insert image description here
The second method is to obtain the value of the selected radio button by clicking other buttons.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    

    RadioGroup radioGroup;
    //提交
    Button bt_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //点击事件
        clickEvent();
    }

    private void initView() {
    
    
        radioGroup = findViewById(R.id.radioGroup);
        bt_submit = findViewById(R.id.bt_submit);
    }

    private void clickEvent() {
    
    
        //提交
        bt_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()) {
    
    
            case R.id.bt_submit:
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
    
    
                    RadioButton r = (RadioButton) radioGroup.getChildAt(i);
                    if (r.isChecked()) {
    
    
                        Log.i("单击其他按钮时获取", "选择性别为:" + r.getText());
                    }
                }
                break;
        }
    }
}

Display of the log effect of determining when other buttons are clicked to obtain the value of the selected radio button:
Insert image description here
The third method is to determine which radio button's ID the clicked ID is, and obtain the value through the ID.

public class MainActivity extends AppCompatActivity {
    
    

    RadioGroup radioGroup;
    //男
    RadioButton radio_man;
    //女
    RadioButton radio_female;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //点击事件
        clickEvent();
    }

    private void initView() {
    
    
        radioGroup = findViewById(R.id.radioGroup);
        radio_man = findViewById(R.id.radio_man);
        radio_female = findViewById(R.id.radio_female);
    }

    private void clickEvent() {
    
    
        //给RadioGroup绑定监视器
        radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
    }

    //单选按钮监听
    private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
    
    
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
            // 选中状态改变时被触发
            switch (checkedId) {
    
    
                case R.id.radio_female:
                    // 当用户选择女性时
                    Log.i("判断点击Id的单选按钮", "选择性别为:" + radio_female.getText().toString());
                    break;
                case R.id.radio_man:
                    // 当用户选择男性时
                    Log.i("判断点击Id的单选按钮", "选择性别为:"+radio_man.getText().toString());
                    break;
            }
        }
    }
}

Determine the clicked radio button log effect display:
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_58159075/article/details/124343635