Android控件RadioButton的使用方法
本文實(shí)例為大家分享了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 //定義一個(gè)單選按鈕組
android:id="@+id/rg_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton //單選按鈕一 使用默認(rèn)樣式
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 使用默認(rèn)樣式
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();//吐司一下被選中的文本值
});
}
}
運(yùn)行效果

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android單選按鈕RadioButton的使用詳解
- Android控件RadioButton實(shí)現(xiàn)多選一功能
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android RadioButton 圖片位置與大小實(shí)例詳解
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android中設(shè)置RadioButton在文字右邊的方法實(shí)例
- android RadioButton和CheckBox組件的使用方法
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android控件系列之RadioButton與RadioGroup使用方法
相關(guān)文章
Android ListView ImageView實(shí)現(xiàn)單選按鈕實(shí)例
這篇文章主要介紹了Android ListView ImageView實(shí)現(xiàn)單選按鈕的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)類似IOS右滑返回的效果(原因分析及解決辦法)
這篇文章主要介紹了Android實(shí)現(xiàn)類似IOS右滑返回的效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03
Android開發(fā)中調(diào)用系統(tǒng)相冊(cè)上傳圖片到服務(wù)器OPPO等部分手機(jī)上出現(xiàn)短暫的顯示桌面問題的解決方法
這篇文章主要介紹了Android開發(fā)中調(diào)用系統(tǒng)相冊(cè)上傳圖片到服務(wù)器OPPO等部分手機(jī)上出現(xiàn)短暫的顯示桌面問題的解決方法,需要的朋友可以參考下2016-12-12
Android 詳解Studio引用Library與導(dǎo)入jar
這篇文章主要介紹了Android Studio引用Library與導(dǎo)入jar的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android檢查手機(jī)網(wǎng)絡(luò)狀態(tài)及網(wǎng)絡(luò)類型的方法
這篇文章主要介紹了Android檢查手機(jī)網(wǎng)絡(luò)狀態(tài)及網(wǎng)絡(luò)類型的方法,涉及Android檢測(cè)手機(jī)網(wǎng)絡(luò)狀態(tài)的技巧,需要的朋友可以參考下2015-04-04
Android APK文件在電腦(PC虛擬機(jī))上面運(yùn)行方法
APK是Android系統(tǒng)的發(fā)布的工程包,很多時(shí)候我們想在電腦上而非Android手機(jī)上面運(yùn)行它,需要的朋友可以了解下2012-12-12
ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android如何調(diào)用系統(tǒng)相機(jī)拍照
這篇文章主要為大家詳細(xì)介紹了Android如何調(diào)用系統(tǒng)相機(jī)拍照的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android實(shí)現(xiàn)圖片區(qū)域裁剪功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片區(qū)域裁剪功能,調(diào)用相冊(cè)、拍照實(shí)現(xiàn)縮放、切割圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

