android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果,运行效果截图如下:

CheckBox控件的定义,main.xml内容如下:


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <EditText
  8. android:id="@+id/editText1"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="请选择"
  12. />
  13. <CheckBox
  14. android:id="@+id/beijing"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="北京"
  18. />
  19. <CheckBox
  20. android:id="@+id/shanghai"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="上海"
  24. />
  25. <CheckBox
  26. android:id="@+id/shenzhen"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="深圳"
  30. />
  31. </LinearLayout>

activity CheckBoxTest.java内容如下:


  1. package checkbox.pack;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.CheckBox;
  5. import android.widget.CompoundButton;
  6. import android.widget.EditText;
  7. public class CheckBoxTest extends Activity {
  8. //对控件对象进行声明
  9. CheckBox beijing=null;
  10. CheckBox shanghai=null;
  11. CheckBox shenzhen=null;
  12. EditText editText1=null;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. //通过控件的ID来得到代表控件的对象
  18. beijing=(CheckBox)findViewById(R.id.beijing);
  19. shanghai=(CheckBox)findViewById(R.id.shanghai);
  20. shenzhen=(CheckBox)findViewById(R.id.shenzhen);
  21. editText1=(EditText)findViewById(R.id.editText1);
  22. //给CheckBox设置事件监听
  23. beijing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  24. @Override
  25. public void onCheckedChanged(CompoundButton buttonView,
  26. boolean isChecked) {
  27. // TODO Auto-generated method stub
  28. if(isChecked){
  29. editText1.setText(buttonView.getText()+"选中");
  30. }else{
  31. editText1.setText(buttonView.getText()+"取消选中");
  32. }
  33. }
  34. });
  35. shanghai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  36. @Override
  37. public void onCheckedChanged(CompoundButton buttonView,
  38. boolean isChecked) {
  39. // TODO Auto-generated method stub
  40. if(isChecked){
  41. editText1.setText(buttonView.getText()+"选中");
  42. }else{
  43. editText1.setText(buttonView.getText()+"取消选中");
  44. }
  45. }
  46. });
  47. shenzhen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  48. @Override
  49. public void onCheckedChanged(CompoundButton buttonView,
  50. boolean isChecked) {
  51. // TODO Auto-generated method stub
  52. if(isChecked){
  53. editText1.setText(buttonView.getText()+"选中");
  54. }else{
  55. editText1.setText(buttonView.getText()+"取消选中");
  56. }
  57. }
  58. });
  59. }
  60. }

android CheckBox控件的定义及事件监听的更多相关文章

  1. Android输入控件EditText和软键盘监听

    1. 跳转到新的页面自动软键盘显示情况: 在配置清单文件AndroidManifest.xml文件,对Activity的windowSoftInputMode属性进行设置. stateUnspecif ...

  2. Android软键盘的隐藏显示、事件监听的代码

    把开发过程中重要的一些内容片段做个珍藏,如下资料是关于Android软键盘的隐藏显示.事件监听的内容,应该是对小伙伴们有所用途. public class ResizeLayout extends L ...

  3. android 防止多次点击,导致事件监听响应到其他界面

    下面有个案例: A点击的时候就跳转到B界面,点击B界面后结束,返回到A界面中 1.此时在B界面中,设置点击事件,点击后结束B v.setOnClickListener(new OnClickListe ...

  4. Java Spring 自定义事件监听

    ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...

  5. Android监听Button和ImageButton控件的点击事件

    一.onClick事件 Button和ImageButton都有一个onClick事件,通过自身的.setOnClickListener(OnClickListener)方法添加点击事件 所有的控件都 ...

  6. android 组合控件接收不到点击事件的问题

    android点击事件的传播是有子控件传给父控件,如果子控件处理过了,父控件不再处理,所以要想让组合控件接收点击事件,必须屏蔽子控件的点击事件. 设置组合控件的clickable和focusable属 ...

  7. Android开发CheckBox控件,全选,反选,取消全选

    在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...

  8. android中RecyclerView控件实现点击事件

    RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...

  9. Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)

    使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();

随机推荐

  1. Spring Mvc 传递参数要controller出现了400,日期参数全局处理,格式化yyyy-MM-dd 和yyyy-MM-dd HH:mm:ss

    描述:今天做一个业务操作的时候,ajax传递参数要controller出现了400,前后台都没有报错. 问题:springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误 解决: ...

  2. 关于多态的理解,有助于理解TStream抽象类的多态机制。

    有的时候 不是很明白流的机制,因为有内存流  文件流 图片流 等等 他们之间的相互转化 靠的就是流的多态性.... unit Unit11; interface uses Winapi.Windows ...

  3. Java反射常用API汇总

    “JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性” 一.类对象的获取 1.通过对象获取 Object obj = ne ...

  4. pandas read excel文件碰到的一个小问题

    今天利用pandas读取excel时,爆出如下错误: 代码为: import pandas as pd db_eua=pd.read_excel('db_eua.xlsx',sheetname='EU ...

  5. Python的程序结构[4] -> 函数/Function[1] -> 内建函数

    内建函数 / Built-in Function or Method Python中有许多的内建函数(查看内建模块部分),此处将对内建函数进行介绍 内建函数 ord / built-in functi ...

  6. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  7. Visio中如何让重叠图形都显示

    如图,两个矩形重叠部分的边框都显示了,是拆分操作,不是组合.

  8. DirectoryServicesCOMException

    捕捉到 System.DirectoryServices.DirectoryServicesCOMException Message=该服务器不愿意处理该请求. Source=System.Direc ...

  9. #define,#undef宏学习

    1.预处理器 1.1预处理符号: __FILE__ :进行编译的源文件名字 __LINE__ :文件当前行的行号 __DATA__ :文件被编译的日期 __TIME__ :文件被编译的时间 __STD ...

  10. java、freemarker保留两位小数

    一.Java保留2位小数 double acc = 22.4322; String accX = String.format("%.2f", acc); 二.freemarker保 ...