一起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()方法为复选框设置了监听器,而监听器的对象就是刚才定义的内部类对象。下面是程序运行时的效果图,请大家参考。

这里写图片描述

各位看官,关于Android中UI控件之CheckBox的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!


猜你喜欢

转载自blog.csdn.net/talk_8/article/details/79773208