Android單選按鈕RadioButton的使用方法
單選按鈕要在一組中選擇一項,并且不能多選。
同一組RadioButton要放在同一個RadioGroup節(jié)點下。
RadioButton默認(rèn)未選中,點擊后選中但是再次點擊不會取消選中。
RadioButton經(jīng)常會更換按鈕圖標(biāo),如果通過button屬性變更圖標(biāo),那么圖標(biāo)與文字就會挨得很近。為了拉開圖標(biāo)與文字之間的距離,得換成drawableLeft屬性展示新圖標(biāo)(不要忘記把button改為@null),再設(shè)置drawablePadding即可指定間隔距離。
復(fù)現(xiàn)代碼時出現(xiàn)了一個錯誤,處理單選按鈕的響應(yīng),要先寫一個單選監(jiān)聽器實現(xiàn)接口 RadioGroup.OnCheckedChangeListener,而不是復(fù)合按鈕的CompoundButton.OnCheckedChangeListener。

MainActivity
package com.example.middle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class RadioVerticalActivity extends AppCompatActivity implements OnCheckedChangeListener {
private TextView tv_marry; // 聲明一個文本視圖對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_vertical);
// 從布局文件中獲取名叫tv_marry的文本視圖
tv_marry = findViewById(R.id.tv_marry);
// 從布局文件中獲取名叫rg_marry的單選組
RadioGroup rg_marry = findViewById(R.id.rg_marry);
// 給rg_marry設(shè)置單選監(jiān)聽器,一旦用戶點擊組內(nèi)的單選按鈕,就觸發(fā)監(jiān)聽器的onCheckedChanged方法
rg_marry.setOnCheckedChangeListener(this);
}
// 在用戶點擊組內(nèi)的單選按鈕時觸發(fā)
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb_married) {
tv_marry.setText("哇哦,祝你早生貴子");
} else if (checkedId == R.id.rb_unmarried) {
tv_marry.setText("哇哦,你的前途不可限量");
}
}
}
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇您的婚姻狀況"
android:textColor="#000000"
android:textSize="17sp" />
<RadioGroup
android:id="@+id/rg_marry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 通過button屬性修改單選按鈕的圖標(biāo) -->
<RadioButton
android:id="@+id/rb_unmarried"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:button="@drawable/radio_selector"
android:text="未婚"
android:textColor="#000000"
android:textSize="17sp" />
<!-- 通過drawableLeft屬性修改單選按鈕的圖標(biāo) -->
<RadioButton
android:id="@+id/rb_married"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:button="@null"
android:drawableLeft="@drawable/radio_selector"
android:drawablePadding="10dp"
android:text="已婚"
android:textColor="#000000"
android:textSize="17sp" />
</RadioGroup>
<TextView
android:id="@+id/tv_marry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="17sp" />
</LinearLayout>
result

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android CheckBox中設(shè)置padding無效解決辦法
- Android開發(fā)之CheckBox的簡單使用與監(jiān)聽功能示例
- Android 中CheckBox多項選擇當(dāng)前的position信息提交的示例代碼
- Android 中CheckBox的isChecked的使用實例詳解
- Android開發(fā)手冊自定義Switch開關(guān)按鈕控件
- Android開關(guān)控件Switch的使用案例
- Android 自定義Switch開關(guān)按鈕的樣式實例詳解
- Android UI控件Switch的使用方法
- Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
相關(guān)文章
android canvas drawText()文字居中效果
這篇文章主要為大家詳細(xì)介紹了android canvas drawText()文字居中效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android實現(xiàn)仿美團、順豐快遞數(shù)據(jù)加載效果
本片文章教給大家用Android實現(xiàn)美團和順豐快遞APP的數(shù)據(jù)加載的動畫效果,有興趣的朋友跟著學(xué)習(xí)嘗試下吧。2017-12-12
Android實例代碼理解設(shè)計模式SOLID六大原則
程序設(shè)計領(lǐng)域, SOLID (單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉(zhuǎn))是由羅伯特·C·馬丁在21世紀(jì)早期 引入的記憶術(shù)首字母縮略字,指代了面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計的基本原則2021-10-10
Android自定義ActionProvider ToolBar實現(xiàn)Menu小紅點
這篇文章主要介紹了Android自定義ActionProvider ToolBar實現(xiàn)Menu小紅點,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

