1.调用setOnCheckedChangeListener()方法,并把 CompoundButton.OnCheckedChangeListener实例作为参数传入
2.在CompoundButton.OnCheckedChangeListener的onCheckedChanged()方法里,取得被选中复选框的实例
/** Called when the activity is first created. */
private CheckBox c1,c2 ;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
c1 = (CheckBox) findViewById(R.id.CheckBox01);
c2 = (CheckBox) findViewById(R.id.CheckBox02);
button = (Button) findViewById(R.id.Button01);
//注册事件监听
c1.setOnCheckedChangeListener(new CheckBoxListener());
c2.setOnCheckedChangeListener(new CheckBoxListener());
button.setOnClickListener(new ButtonClickListener());
}
//定义事件
class CheckBoxListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
//Toast
Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"被选择",Toast.LENGTH_SHORT ).show();
}else{
Toast.makeText(CheckBoxActivity.this, buttonView.getText()+"取消选择",Toast.LENGTH_SHORT ).show();
}
}
}
class ButtonClickListener implements OnClickListener{
String buffer = "";
public void onClick(View v) {
if(c1.isChecked())
buffer = buffer+c1.getText();
if(c2.isChecked())
buffer = buffer +c2.getText();
Toast.makeText(CheckBoxActivity.this, buffer+"被选择", Toast.LENGTH_SHORT).show();
}
}
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox android:text="金钱" android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<CheckBox android:text="地位" android:id="@+id/CheckBox02" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
<Button android:id="@+id/Button01" android:text="确定" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
运行效果
- 大小: 17.7 KB
分享到:
相关推荐
Android开发——listview中嵌套checkBox复选框实现单选、全选删除列表内容 详情可以看我的博客http://blog.csdn.net/jxnk25/article/details/50358231
用Android Studio 2.3.3做的复选框例子,主要用到的代码有:mCheckBox_01 = (CheckBox)findViewById(R.id.CheckBox_01);、boolean b01 = mCheckBox_01.isChecked();、CharSequence s01 = mCheckBox_01.getText();。
Android带复选框的树形组织架构treeListView,类似目录和word的结构图,可折叠,带有两种全选模式:1、子节点选中则父节点选中,适合多级多item下方便了解哪些被选中;2、子节点全部选中父节点才选中,更符合逻辑,...
Eclipse V4.2.0编写的Android复选框(仿购物车)应用实例。
最近公司需求做个员工报餐界面用到多个CheckBox 行列
android用CheckBox做为显示密码的复选框效果
Android 复选框按钮特效,效果不错
NULL 博文链接:https://andy2019.iteye.com/blog/1537355
主要为大家详细介绍了Android中CheckBox复选框控件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本程序实现了listView的item右侧添加复选框的实力,实现全选、全不选等功能,大家可以借鉴下~,有问题欢迎指正。
一个项目用Swing写了一个节点带有Checkbox的树状控件,并有3种工作方式。拿出来和大家共享。SWT的Tree控件本身就可以在节点上加上Checkbox。这也是我喜欢SWT的一个原因。 顺带的,也尝试了一下把一个AWT/Swing控件加...
CheckBox各种炫酷效果我们知道HTML默认的复选框样式十分简陋,而以图片代替复选框的美化方式会给页面表单的处理带来麻烦,那么本文将结合实例带您一起了解一下使用CSS3将复选框checkbox进行样式美化,并且带上超酷的...
Android 练习单选 复选框的用法,以选择性别、爱好者常规参数,来演示checkbox、radiobox的使用方法: 性别单选菜单项组 菜单若编组就是单选菜单项组 ,设置GENDER_GROUP组是可选择的,互斥的, ok....
单选框和复选框 单选按钮和复选按钮都是普通按钮Button的子类,所以可以使用所有Button的方法和属性。也有自己特有的属性方法 单选框 单选框就是在多个选项中只选择一个。 在Android中,单选按钮用RadioButton表示,...
包括一个全局的全选按钮,每一项的全选按钮,单选按钮。实现类似淘宝购物车选中功能
带编辑框复选框解决了复用问题的listview
react-native-checkbox-form是一个简单的复选框组件,它可以在Android和iOS上运行,在checkbox-form中使用Icon取决于 , 演示效果 安装 npm install react-native-checkbox-form --save 复选框形式的部分取决于 ,...
同一个Activity中有两个ListView,且两个ListView中都有CheckBox。容易出来CheckBox无法选中的问题,如果你能确定你的代码没有错误的情况下,请检查你的XMl布局文件。
通过listview显示所有app,解决了checkbox产生的bug