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. <span style="white-space:pre">  </span>@Override
  3. public void onClick(View v) {
  4. // TODO Auto-generated method stub
  5. if(checkbox.isChecked()){
  6. button1.setEnabled(true);
  7. }else{
  8. <span style="white-space:pre">  </span>button1.setEnabled(false);
  9. }
  10. <span style="white-space:pre">  </span>}
  11. });

定义多个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. <CheckBox
  10. android:id="@+id/checkbox2"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignBaseline="@+id/checkbox3"
  14. android:layout_alignBottom="@+id/checkbox3"
  15. android:layout_marginLeft="27dp"
  16. android:layout_toRightOf="@+id/checkbox3"
  17. android:text="面包" />
  18. <CheckBox
  19. android:id="@+id/checkbox3"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignBaseline="@+id/checkbox1"
  23. android:layout_alignBottom="@+id/checkbox1"
  24. android:layout_toRightOf="@+id/button1"
  25. 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. private CheckBox.OnCheckedChangeListener checkboxlister = new CheckBox.OnCheckedChangeListener(){
  10. @Override
  11. public void onCheckedChanged(CompoundButton buttonView,
  12. boolean isChecked) {
  13. // TODO Auto-generated method stub
  14. String str0 = "所选:";
  15. String str1 = "牛仔";
  16. String str2 = "面包";
  17. String str3 = "黄油";
  18. //在这里进行你需要的逻辑
  19. if(checkbox.isChecked()){
  20. tview.setText(str0+str1);
  21. }
  22. if(checkbox2.isChecked()){
  23. tview.setText(str0+str2);
  24. }
  25. if(checkbox3.isChecked()){
  26. tview.setText(str0+str3);
  27. }
  28. }
  29. };

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

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

android CheckBox的运用的更多相关文章

  1. xamarin android checkbox自定义样式

    xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 <?xml version="1.0" encod ...

  2. android CheckBox控件的定义及事件监听

    http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...

  3. Android checkbox 自定义点击效果

    安卓默认的效果                         自定义后的效果 前面的图片当然可以自己修改. 实现这个效果的步骤如下 1.建立 一个selector 的xml <?xml ver ...

  4. Android Checkbox Example

    1. Custom String 打开 “res/values/strings.xml” 文件, File : res/values/strings.xml <?xml version=&quo ...

  5. Android checkBox

    checkBox      状态:选中(true),未选中(false)      属性:           checked="true/false"; private Chec ...

  6. Android checkbox和radiobutton 以及Toast和AlertDialog的使用

    package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...

  7. android CheckBox RadioButton 照片和文字的间距问题

    利用自身的定义CheckBox 要么RadioButton时间.定义自己的图标和文字在不同的手机显示不同的音高.有时不太好控制,下面是我自己的定义CheckBox: 在Layout在下面xml: &l ...

  8. android checkbox 未选中状态 已选中状态 替换成自己的图片

    效果图: 未选中状态: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  9. android CheckBox与监听

    <CheckBox  android:id="@+id/cb1"  android:layout_width="fill_parent"  android ...

随机推荐

  1. [学习笔记] 七步从AngularJS菜鸟到专家(4和5):指令和表达式 [转]

    这一篇包含了"AngularJS - 七步从菜鸟到专家"系列的第四篇(指令)和第五篇(表达式). 之前的几篇展示了我们应用的核心组件,以及如何设置搭建一个Angular.js应用.在这一部分,我们会厘 ...

  2. 树状DP (poj 2342)

    题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[ ...

  3. POI 读取word (word 2003 和 word 2007) (转)

    最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...

  4. React Native填坑之旅--Button篇

    从React过来,发现React Native(以下简称RN)居然没有Button.隔壁的iOS是有UIButton的,隔壁的隔壁的Android里也是有的.没有Button,就没有点击效果啊.这还真 ...

  5. 占位符(placeholder text)

    占位符(placeholder text)是用户在input(输入)框输入任何东西之前放置在input(输入)框中的预定义文本. 你可以用如下方式创建占位符: <input type=" ...

  6. 4.20-4.24程序设计基础结束,UID课程

    通过其他方式实现string函数的效果,效果有比较数组字符.显示字符串长度.复制字符串等.在比较字符串的时候,首先是比较字符串的长度,当长度一样的时候进行不同位置上一一对应的字符比较大小.关于字符长度 ...

  7. jQuery遮罩层效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. Java正则表达式的应用

    在很多种情况下,我们都必须对字符串进行匹配,以便判断字符串的格式是否符合要求,对字符串中的内容进行提取.比如,我要从一段话aabdfe中,判断这段话是否有包含ab这个词,那么如果用if-else来判断 ...

  9. Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...

  10. HTML Meta标签知多少

    文章已同步至个人Blog:Benjamin - 专注前端开发和用户体验 一.基本属性 标签常常被用来定义HTML文档的元数据或者HTTP协议的指向,这些元数据常用在SEO.HTML Pages or ...