[基礎控件]---狀態切換控件CompoundButton及其子類CheckBox、RadioButton、ToggleButton、switch事件監聽與場景使用


一、事件監聽

對於普通的Button,對其進行事件監聽Google官方給出了常見的三種監聽方式:1、對每一個button設置事件監聽器button.setOnClickListener(View.OnclickListener  listener);此種方法當button按鈕較多時代碼顯得多、亂、不夠簡潔明了。

2、在Activity中實現接口View.OnclickListener,然后重寫void onClick(View v)方法,在方法中通過switch(v.getId())予以區分不同Button。此種方法較為簡潔,但是需要實現View.OnclickListener接口。3、在xml布局中在想要被監聽的

button上添加屬性:android:onClick=”doClick”屬性。在Activity 中添加監聽方法public void doClick(View view){},此種方法書寫簡單、明了、不需要實現額外的接口。推薦使用此種方法。也是Google官方文檔中常見用法。

對於狀態切換控件CompoundButton,不僅要對事件觸發的監聽,還有對狀態切換的監聽。所以在CompoundButton中需要對其進行兩個監聽:事件觸發、狀態切換。監聽的方式與普通Button三種監聽方式相似。只不過是多了一個監聽狀態的一項

而已。說多了都是廢話,還是直接上碼。

場景一:對UI界面上多個CompoundButton的事件監聽做統一處理。

<ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:textOff="關"
        android:textOn="開" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkbox_meat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="肉" />

        <CheckBox
            android:id="@+id/checkbox_cheese"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="奶" />
    </LinearLayout>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radiobutton_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="增" />

        <RadioButton
            android:id="@+id/radiobutton_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="刪" />

        <RadioButton
            android:id="@+id/radiobutton_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="改" />

        <RadioButton
            android:id="@+id/radiobutton_seach"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="doClick"
            android:text="查" />
    </RadioGroup>

有了布局,下面上Java代碼對其所有的CompoundButton控件進行統一監聽

/**
     * 向上轉型的目的是為了獲取子控件當前狀態。
     * @param view
     */
    public void doClick(View view) {
        //1、被選中:toogle中isChecked==on
        boolean isChecked=((CompoundButton)view).isChecked();//向上轉型:獲取當前狀態
        //2、被點擊
        switch (view.getId()) {
        case R.id.togglebutton:
            if(isChecked){
                Log.i("MyInfo", "開");
            }else{
                Log.i("MyInfo", "關");
            }
            break;
        case R.id.checkbox_meat:
            if(isChecked){
                Log.i("MyInfo", "肉被選中");
            }else{
                Log.i("MyInfo", "肉被取消");
            }
            break;
        case R.id.checkbox_cheese:
            
            break;
        case R.id.radiobutton_add://切記:RadioButton無狀態的切換,僅有按鈕的切換。所以僅需判斷選中狀態 if(isChecked)
            if(isChecked)
            break;
        case R.id.radiobutton_delete:
            if(isChecked)
            
            break;
        case R.id.radiobutton_update:
            if(isChecked)
            break;
        case R.id.radiobutton_seach:
            if(isChecked)
            break;
        default:
            break;
        }
    }

在doClick()方法中總體上執行了兩個步驟:1被選中---->2被點擊。通常這兩個步驟先后順序應該為被點擊----->被選中。但是這樣需要對每一個子控件分支中都需要添加是否被選中的判斷,代碼顯得重復。

所以在此我們使用逆向被點擊----->被選中。在被選中這一步中使用一個向上轉型是為了可以獲取所有CompoundButton子類的狀態。如果直接強轉為某一具體子類,則不具備通用性,不適應判斷所有CompoundButton

子類的被選中狀態。

當UI界面中狀態切換控件CompoundBuuton與普通Button均存在的情況下,建議對兩種控件的使用不同的方法進行監聽,例如:android:onClick=”compoundButtonClick”與android:onClick=”buttonClick”

 

二、CompoundButton擴展

---未完待續


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM