RadioButton(單選按鈕)在Android開(kāi)發(fā)中應(yīng)用的非常廣泛,比如一些選擇項(xiàng)的時(shí)候亿卤,會(huì)用到單選按鈕。它是一種單選框雙狀態(tài)的按鈕,可以選擇或不選擇。在RadioButton沒(méi)有被選中時(shí)胳螟,用戶能夠按下或點(diǎn)擊來(lái)選中它。
RadioGroup和RadioButton的關(guān)系:
1迁沫、RadioButton表示單個(gè)圓形單選框写妥,而RadioGroup是可以容納多個(gè)RadioButton的容器
2、每個(gè)RadioGroup中的RadioButton同時(shí)只能有一個(gè)被選中
3富纸、不同的RadioGroup中的RadioButton互不相干囤踩,即如果組A中有一個(gè)選中了,組B中依然可以有一個(gè)被選中
4晓褪、大部分場(chǎng)合下堵漱,一個(gè)RadioGroup中至少有2個(gè)RadioButton
5、大部分場(chǎng)合下涣仿,一個(gè)RadioGroup中的RadioButton默認(rèn)會(huì)有一個(gè)被選中勤庐,并建議您將它放在RadioGroup中的起
始位置
注: RadioGroup繼承至LinearLayout,所以LinearLayout的屬性RadioGroup都可以使用好港。
RadioButton特殊屬性:
- android:drawable 設(shè)置圖片可以選著圖片位置
- android:checked 控件是否選中
- android:button 隱藏圓圈
注:button基本屬性就不做細(xì)講
XML 代碼:
<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_width="match_parent"
android:layout_height="@dimen/activity_main_radiogroup_height"
android:background="@color/colorWhite"
android:orientation="horizontal">
<RadioButton
android:id="@+id/main_radiobutton_weixing"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/activity_main_radiobutton_text_color"
android:textSize="@dimen/activity_main_radiobutton_text_size"
android:text="@string/main_radiobutton_weixing"
android:drawableTop="@drawable/radiobutton_main_weixing"
android:gravity="center"
android:checked="true"
android:button="@null"
android:background="@null"/>
<RadioButton
android:id="@+id/main_radiobutton_contacts"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/activity_main_radiobutton_text_color"
android:textSize="@dimen/activity_main_radiobutton_text_size"
android:text="@string/main_radiobutton_contects"
android:drawableTop="@drawable/radiobutton_main_contacts"
android:gravity="center"
android:button="@null"
android:background="@null"/>
<RadioButton
android:id="@+id/main_radiobutton_find"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/activity_main_radiobutton_text_color"
android:textSize="@dimen/activity_main_radiobutton_text_size"
android:text="@string/main_radiobutton_find"
android:drawableTop="@drawable/radiobutton_main_find"
android:gravity="center"
android:button="@null"
android:background="@null"/>
<RadioButton
android:id="@+id/main_radiobutton_my"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="@color/activity_main_radiobutton_text_color"
android:textSize="@dimen/activity_main_radiobutton_text_size"
android:text="@string/main_radiobutton_my"
android:drawableTop="@drawable/radiobutton_main_my"
android:gravity="center"
android:button="@null"
android:background="@null"/>
</RadioGroup>
Activity監(jiān)聽(tīng)代碼:
private void monitoringRadioGrop(){
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.main_radiobutton_weixing:
showToast(R.string.main_radiobutton_weixing);
break;
case R.id.main_radiobutton_contacts:
showToast(R.string.main_radiobutton_contects);
break;
case R.id.main_radiobutton_find:
showToast(R.string.main_radiobutton_find);
break;
case R.id.main_radiobutton_my:
showToast(R.string.main_radiobutton_my);
break;
default:
Log.d(TAG,"怎么監(jiān)聽(tīng)的????");
break;
}
}
});
}
圖片:radiobutton_main_weixing 實(shí)現(xiàn)愉镰,在res/drawable新建XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 未選中-->
<item android:drawable="@drawable/radiobutton_main_weixing_no" android:state_checked="false" />
<!--選中-->
<item android:drawable="@drawable/radiobutton_main_weixing_yes" android:state_checked="true" />
</selector>
文本顏色修改:activity_main_radiobutton_text_color,在res下新建color文件夾在新建XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="@color/button_app_main_yes_text"/>
<!-- not selected -->
<item android:state_checked="false"
android:color="@color/button_app_main_no_text"/>
</selector>
效果圖:
希望對(duì)你們有幫助钧汹,O(∩_∩)O謝謝U商健!拔莱!