天天看點

一起Talk Android吧(第七十一回:Android中UI控件之CheckBox)

各位看官們,大家好,上一回中咱們說的是Android中UI控件之RadioButton的例子,這一回咱們說的例子是UI控件之CheckBox。閑話休提,言歸正轉。讓我們一起Talk Android吧!

看官們,單選按鈕使用的地方非常多,不過也有它的局限性。比如想選擇多個内容時它就無能為力了,是以我們在本章回中介紹一種新控件:

CheckBox

。CheckBox也叫複選框。它通常用來給使用者提供多選操作,程式依據使用者選擇的操作來做不同的事情。它可以看作是單選按鈕的互補。接下來我們通過代碼結合文本的方式來示範使用使用這種元件。

  • 1.在布局中添加

    CheckBox

    。通常是在Activity或者Fragment的布局檔案中添加。
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView //這個文本控件是用來給使用者選擇的内容做提示,提示的内容就是文本顯示的内容
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Interest: "/>
        <CheckBox  //添加CheckBox控件,并且設定它的id等屬性
            android:id="@+id/check_box_bj"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bei Jing"/>

        <CheckBox  //添加CheckBox控件,并且設定它的id等屬性
            android:id="@+id/check_box_sh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Shang Hai"/>
        <CheckBox  //添加CheckBox控件,并且設定它的id等屬性
            android:id="@+id/check_box_gz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Guang Zhou"/>
    </LinearLayout>
           
  • 2.在代碼中擷取布局檔案中的

    CheckBox

    。通常位于Activity或者Fragment的onCreate方法中。
private CheckBox mCheckBoxBJ = (CheckBox)findViewById(R.id.check_box_bj);
    private CheckBox mCheckBoxSH = (CheckBox)findViewById(R.id.check_box_sh);
    private CheckBox mCheckBoxGZ = (CheckBox)findViewById(R.id.check_box_gz);
           
  • 3.在代碼中擷取使用者選擇了哪幾個

    CheckBox

    。通常是在

    CheckBox

    的監聽器中來完成該操作。
private class checkBoxChangeListenerImpl implements CompoundButton.OnCheckedChangeListener{
        private String mString;
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            switch ( compoundButton.getId() ){
                case R.id.check_box_bj:
                    mString = new String("Bei Jing");
                    break;
                case R.id.check_box_gz:
                    mString = new String("Guang Zhou");
                    break;
                case R.id.check_box_sh:
                    mString = new String("Shang Hai");
                    break;
                    default:
                        break;
            }
            if(b) {
                Toast.makeText(getApplicationContext(), mString, Toast.LENGTH_LONG).show();
            }
        }
           

我們首先定義了一個内部類,該類實作了

CompoundButton.OnCheckedChangeListener

接口,并且重寫了該接口的

onCheckedChanged

()方法。該方法的第一個參數表示複選框,第二個參數表示複選框是否被選擇。在上面的代碼中我們對複選框的id進行了比對,并且依據複選框是否被選中彈出一個

Toast

給使用者做提示。接下來我們為複選框設定監聽器。

mCheckBoxBJ.setOnCheckedChangeListener(new checkBoxChangeListenerImpl());
mCheckBoxGZ.setOnCheckedChangeListener(new checkBoxChangeListenerImpl());
mCheckBoxSH.setOnCheckedChangeListener(new checkBoxChangeListenerImpl());
           

從上面的代碼中可以看到,我們使用

setOnCheckedChangeListener

()方法為複選框設定了監聽器,而監聽器的對象就是剛才定義的内部類對象。下面是程式運作時的效果圖,請大家參考。

一起Talk Android吧(第七十一回:Android中UI控件之CheckBox)

各位看官,關于Android中UI控件之CheckBox的例子咱們就介紹到這裡,欲知後面還有什麼例子,且聽下回分解!

繼續閱讀