官术网_书友最值得收藏!

7.5 復(fù)選框

“復(fù)選框”有兩個用途:一是多個“選項”,可以為用戶提供多個選擇項;二是單個“選項”,可以為用戶提供兩種狀態(tài)切換的控件,類似于ToggleButton和Switch。

7.5.1 CheckBox

在Android中,“復(fù)選框”是CheckBox, CheckBox默認(rèn)樣式如圖7-20所示。

圖7-20 CheckBox默認(rèn)樣式

CheckBox對應(yīng)類是android.widget.CheckBox,類圖如圖7-21所示,從圖中可見android.widget.CheckBox繼承了android.widget.Button,這說明CheckBox是一種按鈕。CheckBox有一個特有屬性android:checked,該屬性設(shè)置CheckBox的選中狀態(tài)。它是一個布爾值,true表示選中,false表示未選中。

圖7-21 CheckBox類圖

7.5.2 實例:使用復(fù)選框

圖7-22是使用復(fù)選框?qū)嵗谄聊簧铣霈F(xiàn)了4個CheckBox選項,可以單擊進行選擇。

圖7-22 使用復(fù)選框?qū)嵗?/p>

布局文件activity_main.xml代碼如下:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
          …
          <CheckBox
              android:id="@+id/CheckBox01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:checked="true"                            ①
              android:text="@string/checkBox01"
              android:textSize="@dimen/size"/>
          <CheckBox
              android:id="@+id/CheckBox02"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/checkBox02"
              android:textSize="@dimen/size"/>
          <CheckBox
              android:id="@+id/CheckBox03"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/checkBox03"
              android:textSize="@dimen/size"/>
          <CheckBox
              android:id="@+id/CheckBox04"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/checkBox04"
              android:textSize="@dimen/size"/>
      </LinearLayout>

在布局文件中聲明了4個CheckBox,代碼第①行android:checked="true"是設(shè)置CheckBox01復(fù)選框被選中。

MainActivity.java代碼如下:

        public class MainActivity extends AppCompatActivity
                implements CompoundButton.OnCheckedChangeListener{                         ①

            CheckBox mCheckbox1, mCheckbox2, mCheckbox3, mCheckbox4;
            TextView mTextView;

            @Override
            protected void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                mTextView=(TextView) findViewById(R.id.TextView01);
                //注冊CheckBox01 監(jiān)聽器
                mCheckbox1 =(CheckBox) findViewById(R.id.CheckBox01);
                mCheckbox1.setOnCheckedChangeListener(this);                               ②

                //注冊CheckBox2 監(jiān)聽器
                mCheckbox2 =(CheckBox) findViewById(R.id.CheckBox02);
                mCheckbox2.setOnCheckedChangeListener(this);

                //注冊CheckBox03 監(jiān)聽器
                mCheckbox3 =(CheckBox) findViewById(R.id.CheckBox03);
                mCheckbox3.setOnCheckedChangeListener(this);

                //注冊CheckBox04監(jiān)聽器
                mCheckbox4 =(CheckBox) findViewById(R.id.CheckBox04);
                mCheckbox4.setOnCheckedChangeListener(this);
            }
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){    ③
                CheckBox ckb =(CheckBox)buttonView;
                mTextView.setText(ckb.getText());
            }
        }

上述代碼第①行是聲明MainActivity實現(xiàn)CompoundButton.OnCheckedChangeListener接口,這樣MainActivity就成為了CheckBox事件監(jiān)聽器,該接口要求實現(xiàn)代碼第③行的onCheckedChanged方法。代碼第②行注冊當(dāng)前MainActivity作為CheckBox01監(jiān)聽器,類似其他三個CheckBox都需要注冊。

這4個CheckBox事件處理都是在代碼第③行實現(xiàn)的onCheckedChanged方法中完成,如果每一個CheckBox處理不同,這需要判斷是哪一個CheckBox,類似代碼參考7.4.3節(jié)的RadioButton實例。

注意 CheckBox的事件處理要實現(xiàn)CompoundButton.OnCheckedChangeListener接口,這與RadioButton不同,RadioButton的事件處理者要實現(xiàn)的接口是RadioGroup. OnCheckedChangeListener。他們的事件源差別很大,CheckBox事件源是android. widget.CheckBox,而RadioButton的事件源是android.widget.RadioGroup,不是RadioButton。

主站蜘蛛池模板: 剑川县| 辉县市| 瓮安县| 旺苍县| 临沧市| 武山县| 乌什县| 东光县| 七台河市| 三明市| 常熟市| 虹口区| 青川县| 尤溪县| 同江市| 尉犁县| 高州市| 通化县| 阜康市| 轮台县| 毕节市| 祁门县| 运城市| 犍为县| 农安县| 报价| 军事| 剑河县| 炉霍县| 泸西县| 双桥区| 富阳市| 邢台县| 团风县| 德庆县| 攀枝花市| 中方县| 武邑县| 万年县| 奉节县| 临夏市|