老套路看图:
实现思路:
1.将左边的选择按钮隐藏通过android:button="@null"
2.设置右边的图片即可通过android:drawableRight="@drawable/btn_radio_is_check"
看下核心代码:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rg_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="44dp"
android:gravity="center"
android:text="状态选择"
android:textColor="@color/black"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_turn_on"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/white"
android:button="@null"
android:drawableRight="@drawable/btn_radio_is_check"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="turn_on"
android:textSize="17sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#99000000" />
<RadioButton
android:id="@+id/rb_turn_off"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/white"
android:button="@null"
android:drawableRight="@drawable/btn_radio_is_check"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="turn_off"
android:textSize="17sp" />
</RadioGroup>
在看下drawable:btn_radio_is_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>
在看洁面控制选中的代码:
package com.example.radiobuttonright;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.annotation.Nullable;
/**
* @author xiayiye5
* @date 2021.1.13 11:36
* 类描述 选择添加动作页面
*/
public class SelectActionActivityJava extends Activity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup rgLayout;
private RadioButton rbTurnOn;
private RadioButton rbTurnOff;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_action);
initView();
initData();
}
public void initView() {
rgLayout = findViewById(R.id.rg_layout);
rbTurnOn = findViewById(R.id.rb_turn_on);
rbTurnOff = findViewById(R.id.rb_turn_off);
}
public void initData() {
String switchStatus = getIntent().getStringExtra("switch");
//开关状态回显
if ("turn on".equals(switchStatus)) {
rbTurnOn.setChecked(true);
rbTurnOff.setChecked(false);
} else if ("turn off".equals(switchStatus)) {
rbTurnOff.setChecked(true);
rbTurnOn.setChecked(false);
}
rgLayout.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
Intent intent = new Intent();
if (i == R.id.rb_turn_on) {
rbTurnOn.setChecked(true);
rbTurnOff.setChecked(false);
//回调上个页面
intent.putExtra("switch_status", "turn on");
} else if (i == R.id.rb_turn_off) {
rbTurnOn.setChecked(false);
rbTurnOff.setChecked(true);
intent.putExtra("switch_status", "turn off");
}
setResult(10086, intent);
finish();
}
}
完工,很简单!
如果看着乱,可下载源码区分Kotlin和Java版本
也可以从GitHub下载