最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android控件RadioButton的使用方法

 更新時間:2021年05月12日 10:48:38   作者:緊張的無痕  
這篇文章主要為大家詳細介紹了Android控件RadioButton的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android控件RadioButton的使用代碼,供大家參考,具體內(nèi)容如下

內(nèi)容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RadioActivity">
    <RadioGroup //定義一個單選按鈕組
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton //單選按鈕一 使用默認樣式
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男"
            android:textSize="24sp"
            android:textColor="@color/black"/>
        <RadioButton //單選按鈕2 使用默認樣式
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="24sp"
            android:textColor="@color/black"/>
    </RadioGroup>
    <RadioGroup //組2
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="50dp">
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="男"
            android:button="@null" //無按鈕樣式
            android:textSize="24sp"
            android:background="@drawable/selector_radiobutton" //自定義背景
            android:textColor="@color/black"
            android:checked="true"
            android:gravity="center"/>
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:button="@null" //無按鈕樣式
            android:text="女"
            android:background="@drawable/selector_radiobutton" //自定義背景
            android:textSize="24sp"
            android:textColor="@color/black"/>
    </RadioGroup>
</RelativeLayout>

//selector_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"> //單選被選中的樣式
        <shape android:shape="rectangle">
            <solid android:color="#ff66ff"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false"> //單選沒被選中的樣式
        <shape android:shape="rectangle">
            <stroke android:color="#cc33ff" android:width="2dp"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>
public class RadioActivity extends AppCompatActivity {
 
    private RadioGroup rg_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        rg_1 = findViewById(R.id.rg_1);
        rg_1.setOnCheckedChangeListener((group, checkedId) -> {//設(shè)置組中單選按鈕選中事件
            RadioButton radioButton = findViewById(checkedId);//獲取被選中的id
            Toast.makeText(RadioActivity.this,radioButton.getText(),Toast.LENGTH_SHORT)
                    .show();//吐司一下被選中的文本值
        });
    }
}

運行效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

台州市| 富宁县| 吉林市| 忻州市| 夏邑县| 黄大仙区| 瑞金市| 马边| 福鼎市| 囊谦县| 伊宁市| 九台市| SHOW| 宁国市| 蛟河市| 东丽区| 化隆| 湛江市| 子长县| 天镇县| 繁昌县| 海阳市| 莱芜市| 登封市| 平果县| 海口市| 长汀县| 哈巴河县| 望奎县| 元阳县| 永新县| 孟津县| 漾濞| 井冈山市| 屏南县| 曲麻莱县| 湘阴县| 庐江县| 奉贤区| 河源市| 井冈山市|