Android RadioGroup及RadioButton的使用方法

效果圖:




1、RadioGroup的佈局文件

<RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_below="@+id/ll_title"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="95dp"
            android:layout_height="match_parent"
            android:background="@drawable/top_button_selector"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="待辦"
            android:textColor="@drawable/top_text_selector"
            android:textSize="14dp" />

        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="95dp"
            android:layout_height="match_parent"
            android:background="@drawable/top_button_selector_1"
            android:button="@null"
            android:gravity="center"
            android:text="已辦"
            android:textColor="@drawable/top_text_selector"
            android:textSize="14dp" />
    </RadioGroup>

背景選擇器:名稱爲top_button_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/top_tab_01_bg_hover" android:state_checked="true"></item>
    <item android:drawable="@drawable/top_tab_02_bg" android:state_checked="false"></item>
</selector>

字體選擇器:名稱爲top_text_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_checked="true"></item>
    <item android:color="@color/gray_text" android:state_checked="false"></item>
</selector>

2、在Activity中設置監聽器,並進行相應的操作

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb_1:
					mCurrentType = "undone";
					gdAdapter.addData(notDealgongDanInfoArr, mCurrentType);
					gdAdapter.notifyDataSetChanged();
					break;
				case R.id.rb_2:
					mCurrentType = "done";
					gdAdapter.addData(dealgongDanInfoArr, mCurrentType);
					gdAdapter.notifyDataSetChanged();
					break;
				default:
					break;
				}
			}
		});


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章