Android中提供了一个多选组件CheckBox,实现多选操作,因为可以多选,所以他区别于RadioButton没有了组的概念,要监听用户操作的话需要对每一个CheckBox监听。Android developers里的描述为:public class CheckBox extends CompoundButton,在CompoundButton里有监听方法:

void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) Register a callback to be invoked when the checked state of this button changes.

因此可通过覆写CompoundButton类中的setOnCheckedChangeListener()方法来实现对CheckBox的监听

      《Google Android 应用开发全程实录》(裴佳迪等著)第78页对CheckBox的监听操作为check_button.setOnCheckedChangeListener(new OnCheckChangedListener(){}),这样编译器会一直报错,“OnCheckedChangeListener(){} must implement the inherited abstract method RadioGroup",因为OnCheckedChangeListener()方法是用来监听RaioGroup的。正确的监听操作应为:check_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {})。或者也可使用check_button.setOnClickListener(new OnClickListener() {});来监听。代码如下:

    final CheckBox check_button = (CheckBox)findViewById(R.id.checkBox);    check_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    // TODO Auto-generated method stub    TextView tv = (TextView)findViewById(R.id.text);    tv.setText(check_button.isChecked() ? "This option is checked" : "This option is not checked");    }    });

或者

    check_button.setOnClickListener(new OnClickListener() {    public void onClick(View v) {    // TODO Auto-generated method stub    TextView tv = (TextView)findViewById(R.id.text);    tv.setText(check_button.isChecked() ? "This option is checked" : "This option is not checked");    }});


 

更多相关文章

  1. android中各种图标尺寸以及多分辨率支持方法
  2. [转」android中的数据库操作
  3. android 7.0 系统关闭彩信过CTA测试的方法
  4. Android 缩放、移动、旋转View相关方法
  5. android中的数据库操作ZZ
  6. Android SDK 安装过程及安装失败的处理方法[转]
  7. Android 图片加载Bit地图 OOM异常解决方法

随机推荐

  1. warning: found plain 'id' attribute; d
  2. android:inputType常用取值
  3. android 相对布局属性
  4. Android平台开发-3G function porting-3G
  5. 安卓布局,比较详细也比较乱
  6. Android(安卓)EditText inputType常用属
  7. android:versionCode和android:versionNa
  8. android中inputType属性在EditText输入值
  9. android图形系统详解
  10. Android(安卓)Studio基础篇