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 ...
随机推荐
- 【java】:解析xml
==========================================xml文件<?xml version="1.0" encoding="GB231 ...
- unity --项目总结
最近做的unity的项目涉及到的问题如下: 1.绘制折线图问题: 起初利用的unity自带的linerender组件,这种方法绘制的线不均匀,效果不好.然后又利用画线插件Ves……开头的那个,结果那个 ...
- scrum 4.0
1.准备看板. 形式参考图4. 2.任务认领,并把认领人标注在看板上的任务标签上. 先由个人主动领任务,PM根据具体情况进行任务的平衡. 然后每个人都着手实现自己的任务. 3.为了团队合作愉快进展顺利 ...
- MVC_Ajax请求
MVC_Ajax请求MVC中的AJAX操作原理还是基于Jquery的封装操作.但是吧没有那么恐怖.Ajax.BeginForm:使用Ajax.BeginForm方法会生成一个form表单,最后以Aja ...
- python语法快速入门(1)
http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...
- java线程详解(二)
1,线程安全 先看上一节程序,我们稍微改动一下: //线程安全演示 //火车站有16张票,需要从四个窗口卖出,如果按照上面的多线程实现,程序如下 class Ticket implements Run ...
- UART的CTS与RTS
在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES) 推出了聪明猫(SmartModem)后就有点混淆了.在RS232中RTS 与CTS 是用来半双工模式下的方向切换:HAYES ...
- 第44讲:Scala中View Bounds代码实战及其在Spark中的应用源码解析
今天学习了view bounds的内容,来看下面的代码. //class Pair[T <: Comparable[T]](val first : T,val second : T){// d ...
- mssql的日期函数,如何取得当前年月日,当前时间,当前月份,当前天数,一年中第几天?
mssql函数DATENAME 语法 DATENAME ( datepart , date ) 参数 datepart 是返回的 date 的一部分.下表列出了所有有效的 datepart 参数.用户 ...
- 用c#开发微信 (21) 微信酒店预订系统
本系统主要是帮助酒店让客户可以通过微信预订房间,增加酒店的入住率. 1 微信里订酒店 1.1关注微信号 用微信扫描下面的二维码(微信号 webuscn),关注此微信号 1.2订房 点击微信号里的 微布 ...