RadioButton位于文字右边的显示

先看图:

 实现步骤:

1、首先将左边的按钮隐藏

android:button="@null"

2、设置右边图片

android:drawableRight="@drawable/btn_radio_check"

代码如下:

1、布局代码

    <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 代码(图片可自己在阿里矢量图标库中下载)

<?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选中动作代码

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();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Luuu23

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

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

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

打赏作者

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

抵扣说明:

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

余额充值