Android学习(二)CheckBox 实现
2012-07-02 11:45
260 查看
实现步骤:
1.implements OnCheckedChangListener
2.实例化CheckBox对象
3.对象绑定setOnCheckdChangeListener监听
4.重写监听函数onCheckedChanged(CompoundButton buttonView, boolean isChecked)
buttonView 选中状态发生改变的那个按钮
isChecked 表示按钮新的状态(true/false)
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
package com.luwenjie.CheckBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
//使用状态改变检查监听器
public class CheckBoxProjectActivity extends Activity implements OnCheckedChangeListener {
private CheckBox cb1, cb2, cb3;//创建3个CheckBox对象
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化3个CheckBox
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}
//重写监听器的抽象函数
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//buttonView 选中状态发生改变的那个按钮
//isChecked 表示按钮新的状态(true/false)
if (cb1 == buttonView || cb2 == buttonView || cb3 == buttonView) {
if (isChecked) {
//显示一个提示信息
toastDisplay(buttonView.getText() + "选中");
} else {
toastDisplay(buttonView.getText() + "取消选中");
}
}
}
public void toastDisplay(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
本文出自 “小毛子” 博客,请务必保留此出处http://xiaomaozi.blog.51cto.com/925779/915848
相关文章推荐
- Android学习-实现一个ListView中由Checkbox和TextView构成的布局
- Android学习-ListView+ CheckBox实现单选
- Android学习历程--新闻客户端实现
- Android 带checkbox的listView 实现多选,全选,反选
- 【android控件学习笔记】使用Spinner实现下拉列表
- Android开发学习之录音同步播放的实现
- Android ListView条目全选功能,不用checkbox实现!
- Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现
- Android开发学习之基于ViewPager实现Gallery画廊效果
- 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题
- Android-checkbox实现明文显示密码
- Android使用局和数据实现天气项目-android学习之旅(十二)
- Android在listview添加checkbox实现单选多选操作问题
- Android 带checkbox的listView 实现多选,全选,反选
- Android学习杂记(一)单选框实现单选必须为每个RadioButton添加一个唯一的Id属性
- Android学习之ItemTouchHelper实现RecylerView的拖拽以及滑动删除功能
- android UI进阶之实现listview中checkbox的多选与记录
- #Testin杯#基于android的远程视频监控系统——学习第一步:实现Camera预览
- Android 长按Listview显示CheckBox,实现批量删除。
- Android 下实现带有图标和Checkbox的 ListView Item