RadioGroup和RadioButton(一般用於在選擇中不停切換)

1.佈局代碼

 <!-- radioGRoup樣式 -->
    <style name="ShowMyServiceStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">14sp</item>
        <item name="android:textColor">@color/activity_my_service_name_textcolor_selector</item>
        <item name="android:background">@drawable/activity_my_service_name_selector</item>
    </style>
 <RadioGroup
            android:id="@+id/activity_show_my_service_content_radiogroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/my_service_left_bg_tab"
            android:orientation="vertical" >

            <RadioButton
                android:id="@+id/activity_show_my_service_breakfast"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_breakfast" />

            <View style="@style/gray_underline" 
                />

            <RadioButton
                android:id="@+id/activity_show_my_service_goods"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_goods" />

            <View style="@style/gray_underline" />

            <RadioButton
                android:id="@+id/activity_show_my_service_clean"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_clean" />

            <View style="@style/gray_underline" />

            <RadioButton
                android:id="@+id/activity_show_my_service_maintain"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_maintain" />

            <View style="@style/gray_underline" />

            <RadioButton
                android:id="@+id/activity_show_my_service_laundry"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_laundry" />

            <View style="@style/gray_underline" />

            <RadioButton
                android:id="@+id/activity_show_my_service_morning"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_morning" />

            <View style="@style/gray_underline" />

            <RadioButton
                android:id="@+id/activity_show_my_service_borrow"
                style="@style/ShowMyServiceStyle"
                android:text="@string/activity_show_my_service_borrow" />

            <View style="@style/gray_underline" />
        </RadioGroup>

2.類裏面要實現的代碼

// 給RadioGroup設置監聽器
        mRadioGroupService.setOnCheckedChangeListener(new TabCheckedListener());
        // 設置默認顯示的頁面
        mRadioGroupService.check(R.id.activity_show_my_service_breakfast);
    /**
     * RadioGroup的監聽器實現類
     * 
     * @author Administrator
     * 
     */
    private class TabCheckedListener implements OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            switch (checkedId) {
            case R.id.activity_show_my_service_breakfast:
                mServiceName = "早餐";
                requestServerDatas(mServiceName);
                break;

            case R.id.activity_show_my_service_goods:
                mServiceName = "商品";
                requestServerDatas(mServiceName);
                break;

            case R.id.activity_show_my_service_clean:
                mServiceName = "打掃";
                requestServerDatas(mServiceName);
                break;

            case R.id.activity_show_my_service_borrow:
                mServiceName = "維修";
                requestServerDatas(mServiceName);
                break;

            case R.id.activity_show_my_service_laundry:
                mServiceName = "洗衣";
                requestServerDatas(mServiceName);
                break;

            case R.id.activity_show_my_service_maintain:
                mServiceName = "叫早";
                requestServerDatas(mServiceName);
                break;

            case R.id.activity_show_my_service_morning:
                mServiceName = "借物";
                requestServerDatas(mServiceName);
                break;
            default:
                break;
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章