android CheckBox控件的定义及事件监听
android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果,运行效果截图如下:
CheckBox控件的定义,main.xml内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请选择"
- />
- <CheckBox
- android:id="@+id/beijing"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="北京"
- />
- <CheckBox
- android:id="@+id/shanghai"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="上海"
- />
- <CheckBox
- android:id="@+id/shenzhen"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="深圳"
- />
- </LinearLayout>
activity CheckBoxTest.java内容如下:
- package checkbox.pack;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- public class CheckBoxTest extends Activity {
- //对控件对象进行声明
- CheckBox beijing=null;
- CheckBox shanghai=null;
- CheckBox shenzhen=null;
- EditText editText1=null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通过控件的ID来得到代表控件的对象
- beijing=(CheckBox)findViewById(R.id.beijing);
- shanghai=(CheckBox)findViewById(R.id.shanghai);
- shenzhen=(CheckBox)findViewById(R.id.shenzhen);
- editText1=(EditText)findViewById(R.id.editText1);
- //给CheckBox设置事件监听
- beijing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- editText1.setText(buttonView.getText()+"选中");
- }else{
- editText1.setText(buttonView.getText()+"取消选中");
- }
- }
- });
- shanghai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- editText1.setText(buttonView.getText()+"选中");
- }else{
- editText1.setText(buttonView.getText()+"取消选中");
- }
- }
- });
- shenzhen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked){
- editText1.setText(buttonView.getText()+"选中");
- }else{
- editText1.setText(buttonView.getText()+"取消选中");
- }
- }
- });
- }
- }
android CheckBox控件的定义及事件监听的更多相关文章
- Android输入控件EditText和软键盘监听
1. 跳转到新的页面自动软键盘显示情况: 在配置清单文件AndroidManifest.xml文件,对Activity的windowSoftInputMode属性进行设置. stateUnspecif ...
- Android软键盘的隐藏显示、事件监听的代码
把开发过程中重要的一些内容片段做个珍藏,如下资料是关于Android软键盘的隐藏显示.事件监听的内容,应该是对小伙伴们有所用途. public class ResizeLayout extends L ...
- android 防止多次点击,导致事件监听响应到其他界面
下面有个案例: A点击的时候就跳转到B界面,点击B界面后结束,返回到A界面中 1.此时在B界面中,设置点击事件,点击后结束B v.setOnClickListener(new OnClickListe ...
- Java Spring 自定义事件监听
ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...
- Android监听Button和ImageButton控件的点击事件
一.onClick事件 Button和ImageButton都有一个onClick事件,通过自身的.setOnClickListener(OnClickListener)方法添加点击事件 所有的控件都 ...
- android 组合控件接收不到点击事件的问题
android点击事件的传播是有子控件传给父控件,如果子控件处理过了,父控件不再处理,所以要想让组合控件接收点击事件,必须屏蔽子控件的点击事件. 设置组合控件的clickable和focusable属 ...
- Android开发CheckBox控件,全选,反选,取消全选
在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...
- android中RecyclerView控件实现点击事件
RecyclerView控件实现点击事件跟ListView控件不同,并没有提供类似setOnItemClickListener()这样的注册监听器方法,而是需要自己给子项具体的注册点击事件. 本文的例 ...
- Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)
使用代码主动去调用控件的点击事件(模拟人手去触摸控件) //View 可以是LinearLayout,Button,TextView View.performClick();
随机推荐
- 将ros中suscriber和publisher写入class中
相比于笨拙的全局变量和全局函数,将suscriber和publisher成一个class,形式更加简洁和容易管理,一个节点就是一个类 参考资料 http://answers.ros.org/quest ...
- HDU 1847 【巴什博弈】
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...
- 【 模_板 】 for NOIP 2017
高精度 #include <cstring> #include <cstdio> #define max(a,b) (a>b?a:b) inline void read( ...
- KD-Tree复习笔记(BZOJ1941 & BZOJ2648 & BZOJ4066)
快一年了都没碰到什么必须用KDT的题目导致模板完全忘光了,重新复习了一下. K_Dimention_Tree是一种用来处理二维以上问题的数据结构(OI中一般都是二维),本质是二维启发式估价函数实现剪枝 ...
- [USACO09MAR]Cleaning Up
题目大意: 给你一个长度为n的序列a,你可以将其分为若干段,最终的答案为每一段不同数个数的平方和. 思路: 不难想到一个O(n^2)的DP: f[i]=min{f[j]+cnt(j,i)^2} 考虑一 ...
- 1.13(java学习笔记)异常机制
异常不同于错误,它是程序运行时产生的未知问题. 如果把程序比喻成一辆汽车,那么汽车开着开着突然前面出现了一个大石头挡住了路,这就叫异常. 那么出现了这个异常我们需要去处理,比如打电话给公路管理局,让它 ...
- Android TextView 阴影效果(投影)
Android TextView 阴影效果(投影) 四个参数: 1 2 3 4 android:shadowColor="@color/white" android:shadowD ...
- 细说JavaScript对象(4): for in 循环
如同 in 运算符一样,使用 for in 循环遍历对象属性时,也将往上遍历整个原型链. // Poisoning Object.prototype Object.prototype.bar = 1; ...
- UBI - Unsorted Block Images
参考:http://www.linux-mtd.infradead.org/doc/ubi.html UBI - Unsorted Block Images Table of contents Big ...