0
点赞
收藏
分享

微信扫一扫

Kotlin 使用 RadioGroup & RadioButton 控件

1.新建控件

<!--android:button="@null"去除选中框 -->
			<RadioGroup
            android:id="@+id/rg_set"
            style="?attr/textAppearanceDisplayLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginHorizontal="20dp"
            android:background="?attr/colorOnBackground"
            android:gravity="right"
            android:orientation="horizontal"
            android:switchTextAppearance="@style/TextAppearance.AppCompat.Large"
            android:textColor="?attr/colorOnPrimary">

            <RadioButton
                android:id="@+id/rb_set_ok"
                style="?attr/textAppearanceDisplayLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:background="@drawable/radiopassbtn_bg"
                android:button="@null"
                android:text="OK"
                android:textColor="?attr/colorOnPrimary" />

            <RadioButton
                android:id="@+id/rb_set_nok"
                style="?attr/textAppearanceDisplayLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:background="@drawable/radiofailbtn_bg"
                android:button="@null"
                android:text="NOK"
                android:textColor="?attr/colorOnPrimary" />

            <RadioButton
                android:id="@+id/rb_set_na"
                style="?attr/textAppearanceDisplayLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:background="@drawable/radionabtn_bg"
                android:button="@null"
                android:text="NA"
                android:textColor="?attr/colorOnPrimary" />
        </RadioGroup>

2.新建RadioButton背景

<!-- radiopassbtn_bg.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="?attr/colorOnBackground" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/test_pass" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>

3.设置RadioGroup监听事件

rg_set.setOnCheckedChangeListener(this)

4.获取选中的RadioButton

rg_set.checkedRadioButtonId == R.id.rb_set_ok

5.设置RadioButton为选中状态

view.findViewById<RadioButton>(R.id.rb_callback_ok).isChecked = true

举报

相关推荐

0 条评论