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 ...
随机推荐
- 实现毛玻璃模糊效果/DRNRealTimeBlur
四种方法:1.美工出图 2.coreImage框架,高斯效果 3.ToolBar,覆盖在view上边 //1.添加图片 self.imageView.image = [UIImage imageNa ...
- Android之Fragment学习总结(1)
对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...
- Tomcat需要更改三个端口,才能在一台机器上搭载多个tomcat
1.shutdown端口.port改成与原有的不同即可 2.8080端口,需要更改. 3.默认端口
- 安装完win8后,显卡一些其他的驱动没有被正常安装,此处出现问题的机器是索尼vpccw18fc
问题描述:安装完win8后机器无法正常开启,通过索尼欢迎页面按f10,修复进入,进入后一切正常,分辨率怪异,通过鲁大师检测显卡驱动以及一些细微的驱动没安装,但是鲁大师提示不支持这款显卡驱动,官网也没找 ...
- 采用动态代理方式调用WEB服务(转载+整理)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Oracle数据库字符集修改
Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系.ORACLE支持国家语言的体系结构允许你使用本地化语言来存储,处理,检索数据.一般来说,数据库字符集在安装数据库实例时就 ...
- Magicodes.WeiChat——使用AntiXssAttribute阻止XSS(跨站脚本攻击)攻击
跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者往Web页面里插 ...
- Fiddler:在PC和移动设备上抓取HTTPS数据包
Fiddler是一个免费的Web调试代理,支持任何浏览器.系统以及平台.这个工具是进行Web和App网络开发的必备工具,戳此处下载. 根据Fiddler官网的描述,具有以下六大特点: Web调试 性能 ...
- Xamarin.Android之下拉刷新
一.前言 当今任何一个App中只要存在列表,基本上都会使用下拉刷新,而身为Xamarin一族的我们自然也不会落后,下面笔者将带领大家在Xamarin下实现Android中的下拉刷新的效果. 二.准备工 ...
- 解决dnu restore时的“Cannot handle address family”问题
前几天在使用基于 mono 的 dnx 中的 dnu restore 命令安装 nuget 包包时,遇到了 “Cannot handle address family” 错误,错误详情如下: Cann ...