Android 第十一课——UI Checkbox

常用基础空组件

141150_u8wW_1989321.png

6 CheckBox

复选框是常用组件之一,但是开发过程中虽然很多地方会用到复选框,但是却不会用Android系统提供的原始样式,类似我们在写html时,不同的浏览器提供的复选框按钮是不一样的,兼容性也很差,一般用图片替代伪装。但是无论如何伪装样式,复选框的功能都是一样的。

layout中组件:

       <CheckBox  
            android:id="@+id/cb1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:checked="true" 
            android:text="basketball"/> 
        <CheckBox  
            android:id="@+id/cb2" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="baseball"/>

Activity中组件:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        // 获取checkbox
        cb1 = (CheckBox)findViewById(R.id.cb1);
        cb2 = (CheckBox)findViewById(R.id.cb2);
        // 为checkbox注册事件监听(当复选框被选中或从选中到未被选中状态时这个类的onCheckedChanged方法被触发)
        CBCheckedImpl impl = new CBCheckedImpl();
        cb1.setOnCheckedChangeListener(impl);
        cb2.setOnCheckedChangeListener(impl);
    }
     
  // Activity内部类,实现android.widget.CompoundButton.OnCheckedChangeListener接口同时实现onCheckedChanged方法
  btn表示发生状态变化的组件,flag如果为true表示选中,否则表示为被选中
  @Override 
  public void onCheckedChanged(CompoundButton btn, boolean flag) { 
   // TODO Auto-generated method stub 
   String rs = "nothing"; 
   rs = flag?"选中了"+btn.getText().toString():"未选中"+btn.getText().toString(); 
   Toast.makeText(CheckBoxActivity.this,rs, Toast.LENGTH_SHORT).show(); 
  } 
    
         
  }

注意:

1)复选框的使用基本和单选按钮差不多,主要就是要记住单选按钮实现的接口监听是android.widget.RadioGroup.OnCheckedChangeListener;然而复习按钮实现的监听是android.widget.CompoundButton.OnCheckedChangeListener。不要以为单选按钮和复选按钮都是CompoundButton的子类,就应该实现相同的接口,实际上是因为,单选按钮有组的概念,然而复选按钮没有组的概念。


下一节将学习UI的

7 ProgressBar

8 ListView ListActivity SimpleAdapter


转载于:https://my.oschina.net/heweipo/blog/364677

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值