1. 程式人生 > >RadioButtn單選按鈕詳解

RadioButtn單選按鈕詳解

RadioButtn單選按鈕,常用UI控制元件之一,當需要多選一的時候就需要用到RadioButtn了,通常系統自帶的樣式不能直接使用,大部分都是需要自定義樣式,下面詳細介紹一下如何使用及如何自定義樣式

預覽

效果預覽

基本使用

RadioButtn需要放在RadioGroup中
xml檔案

          <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:
orientation
="horizontal">
<RadioButton android:id="@+id/rb_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rb_left_selector" android:
button
="@null" android:checked="true" android:gravity="center" android:text="" android:textColor="@color/rb_selector" android:textSize="20sp" />
<RadioButton android:
id
="@+id/rb_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rb_right_selector" android:button="@null" android:gravity="center" android:text="" android:textColor="@color/rb_selector" android:textSize="20sp" />
</RadioGroup>

重要屬性介紹:

  • android:orientation=" " horizontal | vertical 水平或垂直顯示
  • android:checked=" " 設定預設選中
  • android:button=" "
    修改自帶圖片的樣式。如果只是單純的圖片,可以直接新增drawable,如果帶文字效果,建議設定為@null並使用android:backgroundandroid:drawable
  • android:background=" " 設定背景
  • android:drawableTop=" "|android:drawableBottom=" "|android:drawableLeft=" "|android:drawableRight=" "
    這四個分別對應顯示在文字的上下左右
  • android:drawablePadding=" "文字距離圖片的距離

圖片切換的selector.xml,放到res/drawable下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@mipmap/rb_left_select"/>
    <item android:state_selected="true" android:drawable="@mipmap/rb_left_select"/>
    <item android:state_checked="true" android:drawable="@mipmap/rb_left_select"/>
    <item android:drawable="@mipmap/rb_left"/>
</selector>

文字顏色切換的selector.xml,這個放到res/color下,如果沒有color請新建

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff"/>
    <item android:state_pressed="true" android:color="#ffffff"/>
    <item android:state_selected="true" android:color="#ffffff"/>
    <item android:color="#000000"/>
</selector>

監聽事件

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // 判斷選中哪個
                switch (checkedId) {
                    case R.id.rb_man:
                        Log.e(TAG, "rb_left");
                        break;
                    case R.id.rb_woman:
                        Log.e(TAG, "rb_right");
                        break;
                    default:
                        break;
                }
            }
        });
        
    //  獲取選中的值
    RadioButton radioButton = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
    tv.setText(radioButton.getText().toString());

注意事項:

  1. 選擇時如果點選沒有切換,都為選中狀態,請為每個radiobutton設定id
  2. 使用android:backgroundandroid:drawable時需要設定android:button="@null"
  3. 如果三個或三個以上選項時,選擇的時候出現兩個同時選中的狀態,在程式碼中設定選中rg.check(R.id.rb_left);

Demo下載地址:TestRadioButton