android CheckBox的运用
CheckBox定义一个同意协议的按钮,只要同意button才可以点击
XML代码
- <CheckBox
- android:id="@+id/checkbox1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/button1"
- android:layout_alignLeft="@+id/linearLayout1"
- android:text="牛仔"
- />
在onClick里面设置只要当checkbox.isChecked()为true,也就是勾选上时,button1.setEnabled(true);才可以点击
java代码
- checkbox = (CheckBox) findViewById(R.id.checkbox1);
- checkbox.setChecked(false);
- button1.setEnabled(false);
- checkbox.setOnClickListener(new CheckBox.OnClickListener(){
- <span style="white-space:pre"> </span>@Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(checkbox.isChecked()){
- button1.setEnabled(true);
- }else{
- <span style="white-space:pre"> </span>button1.setEnabled(false);
- }
- <span style="white-space:pre"> </span>}
- });
定义多个CheckBox来控制同一个控件
XML代码
- <CheckBox
- android:id="@+id/checkbox1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/button1"
- android:layout_alignLeft="@+id/linearLayout1"
- android:text="牛仔"
- />
- <CheckBox
- android:id="@+id/checkbox2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/checkbox3"
- android:layout_alignBottom="@+id/checkbox3"
- android:layout_marginLeft="27dp"
- android:layout_toRightOf="@+id/checkbox3"
- android:text="面包" />
- <CheckBox
- android:id="@+id/checkbox3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/checkbox1"
- android:layout_alignBottom="@+id/checkbox1"
- android:layout_toRightOf="@+id/button1"
- android:text="黄油" />
Java代码
- checkbox = (CheckBox) findViewById(R.id.checkbox1);
- checkbox2 = (CheckBox) findViewById(R.id.checkbox2);
- checkbox3 = (CheckBox) findViewById(R.id.checkbox3);
- //通过OnCheckedChangeListener来设置来个CheckBox对象
- checkbox.setOnCheckedChangeListener(checkboxlister);
- checkbox2.setOnCheckedChangeListener(checkboxlister);
- checkbox3.setOnCheckedChangeListener(checkboxlister);
- }
- private CheckBox.OnCheckedChangeListener checkboxlister = new CheckBox.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- String str0 = "所选:";
- String str1 = "牛仔";
- String str2 = "面包";
- String str3 = "黄油";
- //在这里进行你需要的逻辑
- if(checkbox.isChecked()){
- tview.setText(str0+str1);
- }
- if(checkbox2.isChecked()){
- tview.setText(str0+str2);
- }
- if(checkbox3.isChecked()){
- tview.setText(str0+str3);
- }
- }
- };
也可以使用OnTouchListener(触摸事件)来触发
- checkbox.setOnTouchListener(checktouch);
- checkbox2.setOnTouchListener(checktouch);
- checkbox3.setOnTouchListener(checktouch);
- }
- private CheckBox.OnTouchListener checktouch = new CheckBox.OnTouchListener(){
- @Override
- public boolean onTouch(View arg0, MotionEvent arg1) {
- // TODO Auto-generated method stub
- if(checkbox.isChecked()){
- tview.setText("mimi");
- }else{
- tview.setText("pipi");
- }
- return false;
- }
- };
android CheckBox的运用的更多相关文章
- xamarin android checkbox自定义样式
xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 <?xml version="1.0" encod ...
- android CheckBox控件的定义及事件监听
http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...
- Android checkbox 自定义点击效果
安卓默认的效果 自定义后的效果 前面的图片当然可以自己修改. 实现这个效果的步骤如下 1.建立 一个selector 的xml <?xml ver ...
- Android Checkbox Example
1. Custom String 打开 “res/values/strings.xml” 文件, File : res/values/strings.xml <?xml version=&quo ...
- Android checkBox
checkBox 状态:选中(true),未选中(false) 属性: checked="true/false"; private Chec ...
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...
- android CheckBox RadioButton 照片和文字的间距问题
利用自身的定义CheckBox 要么RadioButton时间.定义自己的图标和文字在不同的手机显示不同的音高.有时不太好控制,下面是我自己的定义CheckBox: 在Layout在下面xml: &l ...
- android checkbox 未选中状态 已选中状态 替换成自己的图片
效果图: 未选中状态: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- android CheckBox与监听
<CheckBox android:id="@+id/cb1" android:layout_width="fill_parent" android ...
随机推荐
- 1057. Stack (30)
分析: 考察树状数组 + 二分, 注意以下几点: 1.题目除了正常的进栈和出栈操作外增加了获取中位数的操作, 获取中位数,我们有以下方法: (1):每次全部退栈,进行排序,太浪费时间,不可取. (2) ...
- 基于选择重传ARQ传输协议的数据重传机制方案设计
原文链接: http://blog.csdn.net/pinghegood/article/details/7841281 1.背景 最近在项目中,由于使用TD网络传输数据,数据掉包严重,软件组老大 ...
- js中json对象的深拷贝
/** * 对象深拷贝 * @param obj */ function deepCopy(obj) { var txt=JSON.stringify(obj); return JSON.parse( ...
- Big Number--hdu1018(数学)
http://acm.hdu.edu.cn/showproblem.php?pid=1018 题目大意 : 求一个数的阶乘的位数 公式log10(n!)=log10(1)+log10(2)+log1 ...
- Coredata 的deletedObjects 方法的使用 附带终止程序的函数
abort();//终止程序 NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity ...
- centos的安装,网络的调试
因业务需要,我今天尝试为公司服务器安装centos,选择的版本是6.7 第一步,下载:http://isoredirect.centos.org/centos/6/isos/x86_64/
- Android性能优化方法(六)
ContentProvider优化改进 1.索引简单的说,索引就像书本的目录,目录可以快速找到所在页数,数据库中索引可以帮助快速找到数据,而不用全表扫描,合适的索引可以大大提高数据库查询的效率.(1) ...
- bootstrap中的Tooltips工具提示的使用问题
在使用bootstrap中的Tooltips时,官方文档中的实例代码若直接放在.container 或 .container-fluid类中时,四个button悬停之后会把button之间的margi ...
- hibernate 不识别union解决方法
问题: 一个表里有 1, 2 1, 3 2, 1 2, 4 现在要找第一位是1的第二位:2,3 和 第二位是1的第一位:2.然后去掉重复 ...
- php_mysqli面向对象链接数据库(一)
<?php$servername="localhost";$username="root";$password="root";$dbn ...