zoukankan      html  css  js  c++  java
  • 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. };  
  • 相关阅读:
    弱网环境测试点总结
    【CMDB】高级配置
    【CMDB】获取服务器数据
    Centos部属前后端项目
    Centos部署项目
    Django
    nginx反向代理和负载均衡
    nginx的配置
    centos7 安装nginx
    centos7 安装Virtualenv
  • 原文地址:https://www.cnblogs.com/android100/p/Android-checkbox.html
Copyright © 2011-2022 走看看