[基础控件]---状态切换控件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扩展

---未完待续

你可能感兴趣的:(RadioButton)