Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 基本控件筆記:RadioButton 與 CheckBox 的用法與樣式自訂

編輯:關於 Android 開發

Android 基本控件筆記:RadioButton 與 CheckBox 的用法與樣式自訂

單選與多選是表單裡最常見的兩種互動,Android 分別用 RadioButton 與 CheckBox 來實作。這篇把基本用法、取值方式與樣式自訂一次講完。

一、基本用法與事件處理

RadioButton 必須放在 RadioGroup 裡,這樣同一組內才會互斥,只能選一個。RadioGroup 上的 orientation 屬性可以控制排列方向。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="請選擇你的性別"
        android:textSize="30sp"/>

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </RadioGroup>

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"/>
</LinearLayout>

取得選中值有兩種做法。

做法 A:監聽 RadioGroup 的選擇變化(推薦,即時反應)

public class MainActivity extends Activity {
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rg1);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                RadioButton radioButton = (RadioButton) findViewById(checkId);
                Toast.makeText(MainActivity.this,
                        "你選中了" + radioButton.getText().toString(),
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}

做法 B:在按下按鈕時遍歷取值(適合送出表單的時機)

public class MainActivity extends Activity {
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rg1);
        Button btn = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                    if (radioButton.isChecked()) {
                        Toast.makeText(MainActivity.this,
                                "你選擇了" + radioButton.getText(),
                                Toast.LENGTH_LONG).show();
                        break;
                    }
                }
            }
        });
    }
}

CheckBox 的用法幾乎與 RadioButton 相同,差別只在於它允許複選,所以不放在 RadioGroup 裡。

<CheckBox android:id="@+id/cb1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="蘋果"/>
<CheckBox android:id="@+id/cb2" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="香蕉"/>
<CheckBox android:id="@+id/cb3" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="梨子"/>

複選的取值要逐一檢查 isChecked(),把勾選的項目串起來:

public class MainActivity extends Activity
        implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private CheckBox checkBox1, checkBox2, checkBox3;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkBox1 = (CheckBox) findViewById(R.id.cb1);
        checkBox2 = (CheckBox) findViewById(R.id.cb2);
        checkBox3 = (CheckBox) findViewById(R.id.cb3);
        btn1 = (Button) findViewById(R.id.btn1);

        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3.setOnCheckedChangeListener(this);
        btn1.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (compoundButton.isChecked()) {
            Toast.makeText(MainActivity.this,
                    "你選中了" + compoundButton.getText(),
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onClick(View view) {
        String choose = "";
        if (checkBox1.isChecked()) choose += checkBox1.getText().toString();
        if (checkBox2.isChecked()) choose += checkBox2.getText().toString();
        if (checkBox3.isChecked()) choose += checkBox3.getText().toString();
        Toast.makeText(MainActivity.this, "你選中了" + choose, Toast.LENGTH_LONG).show();
    }
}

二、自訂勾選框外觀

系統預設的方框在新版 Android 上未必符合設計稿。以 CheckBox 為例,有兩種做法,本質相同,都是換掉 drawable。

做法一:StateListDrawable + button 屬性

先定義一個 selector,用 state_checked 區分勾選與未勾選的圖:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_enabled="true"
        android:drawable="@mipmap/btn_radio_on"/>
    <item android:state_checked="false" android:state_enabled="true"
        android:drawable="@mipmap/btn_radio_off"/>
</selector>

再在版面檔中用 android:button 指到這個 selector 即可。

做法二:自訂 style

第一步同樣先寫好 selector;第二步在 styles.xml 定義一個樣式,把 android:button 指過去,並一併設定文字大小、間距等屬性;第三步在版面檔中用 style="@style/你的樣式名稱" 套用。這種做法的好處是樣式可重複使用,多個畫面共用同一套視覺規格時特別省事。

兩種做法都能達到相同效果,差別只在於是否要抽成可重複使用的樣式。實際效果如下圖所示,有興趣可以自己動手試試每一個屬性的變化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved