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. 实现毛玻璃模糊效果/DRNRealTimeBlur

    四种方法:1.美工出图  2.coreImage框架,高斯效果 3.ToolBar,覆盖在view上边 //1.添加图片 self.imageView.image = [UIImage imageNa ...

  2. Android之Fragment学习总结(1)

    对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...

  3. Tomcat需要更改三个端口,才能在一台机器上搭载多个tomcat

    1.shutdown端口.port改成与原有的不同即可 2.8080端口,需要更改. 3.默认端口

  4. 安装完win8后,显卡一些其他的驱动没有被正常安装,此处出现问题的机器是索尼vpccw18fc

    问题描述:安装完win8后机器无法正常开启,通过索尼欢迎页面按f10,修复进入,进入后一切正常,分辨率怪异,通过鲁大师检测显卡驱动以及一些细微的驱动没安装,但是鲁大师提示不支持这款显卡驱动,官网也没找 ...

  5. 采用动态代理方式调用WEB服务(转载+整理)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Oracle数据库字符集修改

    Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.一般来说,数据库字符集在安装数据库实例时就 ...

  7. Magicodes.WeiChat——使用AntiXssAttribute阻止XSS(跨站脚本攻击)攻击

    跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往Web页面里插 ...

  8. Fiddler:在PC和移动设备上抓取HTTPS数据包

    Fiddler是一个免费的Web调试代理,支持任何浏览器.系统以及平台.这个工具是进行Web和App网络开发的必备工具,戳此处下载. 根据Fiddler官网的描述,具有以下六大特点: Web调试 性能 ...

  9. Xamarin.Android之下拉刷新

    一.前言 当今任何一个App中只要存在列表,基本上都会使用下拉刷新,而身为Xamarin一族的我们自然也不会落后,下面笔者将带领大家在Xamarin下实现Android中的下拉刷新的效果. 二.准备工 ...

  10. 解决dnu restore时的“Cannot handle address family”问题

    前几天在使用基于 mono 的 dnx 中的 dnu restore 命令安装 nuget 包包时,遇到了 “Cannot handle address family” 错误,错误详情如下: Cann ...