●设置复选框的Check状态的时候,调用setChecked()方法

●追加Android复选框被选择时处理的时候,

1.调用setOnCheckedChangeListener()方法,并把CompoundButton.OnCheckedChangeListener实例作为参数传入

2.在CompoundButton.OnCheckedChangeListener的onCheckedChanged()方法里,取得被选中Android复选框的实例

xml布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/background2"    android:orientation="horizontal" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="爱好" />    <CheckBox        android:id="@+id/box1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="足球" >    </CheckBox>    <CheckBox        android:id="@+id/box2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="篮球" >    </CheckBox>    <CheckBox        android:id="@+id/box3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        t=""        android:text="排球" >    </CheckBox>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="选择" /></LinearLayout>

java代码:
package com.xzy.demo.checkbox;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast;public class CheckedBoxActivity extends Activity {private CheckBox box1;private CheckBox box2;private CheckBox box3;private Button button;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);box1 = (CheckBox) findViewById(R.id.box1);box2 = (CheckBox) findViewById(R.id.box2);box3 = (CheckBox) findViewById(R.id.box3);button = (Button) findViewById(R.id.button1);// 复选框被选中的事件OnCheckedChangeListener listener = new OnCheckedChangeListener() {// 表示被选中的复选框和是否被选中public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (isChecked) {Toast.makeText(CheckedBoxActivity.this,buttonView.getText(), 3000).show();}}};// 为box按钮组绑定监听事件box1.setOnCheckedChangeListener(listener);box2.setOnCheckedChangeListener(listener);box3.setOnCheckedChangeListener(listener);// 按钮点击事件button.setOnClickListener(new OnClickListener() {public void onClick(View v) {String text = "";if (box1.isChecked()) {text += box1.getText();}if (box2.isChecked()) {text += box2.getText();}if (box3.isChecked()) {text += box3.getText();}Toast.makeText(CheckedBoxActivity.this, text, 3000).show();}});}}
截图:


更多相关文章

  1. Android定时器实现的几种方法
  2. android BaseAdapter优化
  3. 可下拉的PinnedHeaderExpandableListView的实现
  4. Android:在Eclipse下开发android应用产生的问题及解决方法
  5. android判断软件是否第一次运行的方法
  6. Android(安卓)AlertDialog 无法弹出输入法
  7. [2].FFMpeg入门之 - Android使用FFMpeg命令
  8. android dialog屏蔽back返回键的解决方法
  9. Android简单明了的使用属性动画ObjectAnimator 旋转 平移 渐变

随机推荐

  1. sqlserver CONVERT()函数用法小结
  2. SQL Server 高性能写入的一些经验总结
  3. 磁盘缓存专题之一 缓存命中和缓存未命中&
  4. 学会sql数据库关系图(Petshop)
  5. SQL Server自动更新统计信息的基本算法
  6. SQL Server 数据库备份和还原认识和总结(
  7. SQL Server 数据库备份和还原认识和总结
  8. SQL 查询性能优化 解决书签查找
  9. 编写SQL需要注意的细节Checklist总结
  10. 巧妙的自关联运用