先看图:
实现步骤:
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();
}
}