android CheckBox的运用

CheckBox定义一个同意协议的按钮,只要同意button才可以点击

XML代码 

  1. <CheckBox  
  2.        android:id="@+id/checkbox1"  
  3.        android:layout_width="wrap_content"  
  4.        android:layout_height="wrap_content"  
  5.        android:layout_above="@+id/button1"  
  6.        android:layout_alignLeft="@+id/linearLayout1"   
  7.        android:text="牛仔"  
  8.        />  

 

 

在onClick里面设置只要当checkbox.isChecked()为true,也就是勾选上时,button1.setEnabled(true);才可以点击
java代码 

  1. checkbox = (CheckBox) findViewById(R.id.checkbox1);  
  2. checkbox.setChecked(false);  
  3. button1.setEnabled(false);   
  1. checkbox.setOnClickListener(new CheckBox.OnClickListener(){  
  2.   
  3. <span style="white-space:pre">  </span>@Override  
  4.     public void onClick(View v) {  
  5.     // TODO Auto-generated method stub  
  6.     if(checkbox.isChecked()){  
  7.         button1.setEnabled(true);                 
  8.     }else{  
  9.     <span style="white-space:pre">  </span>button1.setEnabled(false);  
  10.     }  
  11. <span style="white-space:pre">  </span>}  
  12.               
  13. });  


定义多个CheckBox来控制同一个控件

 

XML代码 

  1. <CheckBox  
  2.         android:id="@+id/checkbox1"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_above="@+id/button1"  
  6.         android:layout_alignLeft="@+id/linearLayout1"   
  7.         android:text="牛仔"  
  8.         />  
  9.   
  10.     <CheckBox  
  11.         android:id="@+id/checkbox2"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignBaseline="@+id/checkbox3"  
  15.         android:layout_alignBottom="@+id/checkbox3"  
  16.         android:layout_marginLeft="27dp"  
  17.         android:layout_toRightOf="@+id/checkbox3"  
  18.         android:text="面包" />  
  19.   
  20.     <CheckBox  
  21.         android:id="@+id/checkbox3"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_alignBaseline="@+id/checkbox1"  
  25.         android:layout_alignBottom="@+id/checkbox1"  
  26.         android:layout_toRightOf="@+id/button1"  
  27.         android:text="黄油" />  


Java代码 

  1.     checkbox = (CheckBox) findViewById(R.id.checkbox1);  
  2.     checkbox2 = (CheckBox) findViewById(R.id.checkbox2);  
  3.     checkbox3 = (CheckBox) findViewById(R.id.checkbox3);  
  4.     //通过OnCheckedChangeListener来设置来个CheckBox对象  
  5.     checkbox.setOnCheckedChangeListener(checkboxlister);  
  6.     checkbox2.setOnCheckedChangeListener(checkboxlister);  
  7.     checkbox3.setOnCheckedChangeListener(checkboxlister);  
  8. }  
  9.   
  10. private CheckBox.OnCheckedChangeListener checkboxlister = new CheckBox.OnCheckedChangeListener(){  
  11.   
  12.     @Override  
  13.     public void onCheckedChanged(CompoundButton buttonView,  
  14.             boolean isChecked) {  
  15.         // TODO Auto-generated method stub  
  16.         String str0 = "所选:";  
  17.         String str1 = "牛仔";  
  18.         String str2 = "面包";  
  19.         String str3 = "黄油";  
  20.         //在这里进行你需要的逻辑  
  21.         if(checkbox.isChecked()){  
  22.             tview.setText(str0+str1);  
  23.         }  
  24.         if(checkbox2.isChecked()){  
  25.             tview.setText(str0+str2);  
  26.         }  
  27.         if(checkbox3.isChecked()){  
  28.             tview.setText(str0+str3);  
  29.         }  
  30.     }  
  31.       
  32. };  

 

也可以使用OnTouchListener(触摸事件)来触发 

    1. checkbox.setOnTouchListener(checktouch);  
    2.     checkbox2.setOnTouchListener(checktouch);  
    3.     checkbox3.setOnTouchListener(checktouch);  
    4. }  
    5. private CheckBox.OnTouchListener checktouch = new CheckBox.OnTouchListener(){  
    6.   
    7.     @Override  
    8.     public boolean onTouch(View arg0, MotionEvent arg1) {  
    9.         // TODO Auto-generated method stub  
    10.         if(checkbox.isChecked()){  
    11.             tview.setText("mimi");  
    12.         }else{  
    13.             tview.setText("pipi");  
    14.         }  
    15.         return false;  
    16.     }  
    17.       
    18. };  

转载于:https://www.cnblogs.com/android100/p/Android-checkbox.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值