国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android控件RadioButton的使用方法

瀏覽:5日期:2022-09-18 18:08:34

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

內(nèi)容

Android控件RadioButton的使用方法

<?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 //定義一個(gè)單選按鈕組android: android:layout_width='wrap_content'android:layout_height='wrap_content'android:orientation='vertical'><RadioButton //單選按鈕一 使用默認(rèn)樣式 android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:checked='true' android:text='男' android:textSize='24sp' android:textColor='@color/black'/><RadioButton //單選按鈕2 使用默認(rèn)樣式 android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='女' android:textSize='24sp' android:textColor='@color/black'/> </RadioGroup> <RadioGroup //組2android: 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: /> <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();//吐司一下被選中的文本值}); }}

運(yùn)行效果

Android控件RadioButton的使用方法

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 沾益县| 深泽县| 阆中市| 恩施市| 旺苍县| 中牟县| 蕉岭县| 龙南县| 海阳市| 乌拉特前旗| 衡水市| 秀山| 二连浩特市| 金塔县| 乐安县| 宝山区| 保山市| 疏勒县| 铜陵市| 宿松县| 河曲县| 文山县| 西昌市| 万源市| 寿光市| 龙井市| 商都县| 绵竹市| 扎赉特旗| 博乐市| 灌云县| 阿瓦提县| 嵊泗县| 洛浦县| 乌海市| 二连浩特市| 巨野县| 铁岭市| 龙江县| 运城市| 莲花县|