The RadioButton is located on the right side of the text

Look at the picture first:

 Implementation steps:

1. First hide the button on the left

android:button="@null"

2. Set the picture on the right

android:drawableRight="@drawable/btn_radio_check"

code show as below:

1. Layout code

    <RadioGroup
        android:id="@+id/rg_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/rb_man"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:button="@null"
            android:drawableRight="@drawable/btn_radio_check"
            android:text="男"
            android:textSize="17sp" />
        <RadioButton
            android:id="@+id/rb_woman"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:button="@null"
            android:drawableRight="@drawable/btn_radio_check"
            android:text="女"
            android:textSize="17sp" />
    </RadioGroup>

2. btn_radio_check code (pictures can be downloaded from Ali vector icon library)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/btn_radio_on" android:state_checked="true" />
    <item android:drawable="@mipmap/btn_radio_off" android:state_checked="false" />
</selector>

3. Activity selects the action code

package com.example.shopcode.activity.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.shopcode.R;

/**
 * 选择性别页面
 */
public class EditsexActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    RadioGroup rg_sex;
    RadioButton rb_man,rb_woman;

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

        rg_sex = findViewById(R.id.rg_sex);
        rb_man = findViewById(R.id.rb_man);
        rb_woman = findViewById(R.id.rb_woman);

        initData();
    }

    private void initData() {
        //选择状态回显
        String status = getIntent().getStringExtra("status");
        if ("man".equals(status)) {
            rb_man.setChecked(true);
            rb_woman.setChecked(false);
        } else if ("woman".equals(status)) {
            rb_woman.setChecked(true);
            rb_man.setChecked(false);
        }
        rg_sex.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        Intent intent = new Intent();
        if (i == R.id.rb_man) {
            rb_man.setChecked(true);
            rb_woman.setChecked(false);
            //回到上一页面
            intent.putExtra("status", "man");
        } else if (i == R.id.rb_woman) {
            rb_woman.setChecked(true);
            rb_man.setChecked(false);
            //回到上一页面
            intent.putExtra("status", "woman");
        }
        setResult(10000, intent);
        finish();
    }
}

Guess you like

Origin blog.csdn.net/L73748196_/article/details/126607191