CheckBox 复选框,多选按钮 
/*   * CheckBox 复选框,多选按钮    * 可以提供给用户在多个选项之间实现多选效果   * 一个CheckBox代表多选中的一个选项   */

package com.example.kn_day04_3_checkbox;
import java.util.ArrayList;
import android.app.Activity; import android.os.Bundle; import android.util.Log; 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;
/**  *   实现功能:全选、反选、全不选、获取选中项功能  *  * CheckBox 复选框 多选按钮  * 一个CheckBox代表多选中的一个选项  * 可以提供给用户在多个选项之间 实现多选效果  *  * 注:OnCheckedChangeListener  * 位于android.widget.CompoundButton.OnCheckedChangeListener  *  * @author KNOWN  */ public class CheckBoxActivity extends Activity   implements OnClickListener,   OnCheckedChangeListener  {
 CheckBox cb1;  CheckBox cb2;  CheckBox cb3;  //用于存储当前用户的所有选项  ArrayList strList = new ArrayList();  //存放CheckBox列表 将所有CheckBox放在一个集合中,方便操作  ArrayList cbList = new ArrayList();
 @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_check_box);
  cb1 = (CheckBox) findViewById(R.id.cb1);   cb2 = (CheckBox) findViewById(R.id.cb2);   cb3 = (CheckBox) findViewById(R.id.cb3);   cbList.add(cb1);   cbList.add(cb2);   cbList.add(cb3);     Button btn1 = (Button) findViewById(R.id.btn1);   Button btn2 = (Button) findViewById(R.id.btn2);   Button btn3 = (Button) findViewById(R.id.btn3);   Button btn4 = (Button) findViewById(R.id.btn4);   // 设置单击事件   btn1.setOnClickListener(this);   btn2.setOnClickListener(this);   btn3.setOnClickListener(this);   btn4.setOnClickListener(this);   // 设置选中状态改变事件   cb1.setOnCheckedChangeListener(this);   cb2.setOnCheckedChangeListener(this);   cb3.setOnCheckedChangeListener(this);  }
 @Override  public void onClick( View v) {   // TODO Auto-generated method stub
  switch ( v.getId()) {    // 获取当前相中项   case   R.id.btn1:        Log.i("当前选中了:", strList.toString());    break;    // 全选   case   R.id.btn2:    for (int i = 0; i < cbList.size(); i++) {       cbList.get(i).setChecked(true);    }        break;    //全不选   case   R.id.btn3:    for (int i = 0; i < cbList.size(); i++) {       cbList.get(i).setChecked(false);    }        break;    //反选   case   R.id.btn4:    for (int i = 0; i < cbList.size(); i++) {     //方法1:if判断     /*if(cbList.get(i).isChecked()){      cbList.get(i).setChecked(false);     }else{      cbList.get(i).setChecked(true);     }*/     //方法2:将当前状态改变为相反的状态     cbList.get(i).toggle();    }        break;
  default:    break;   }  }
 /**   *   监听CheckBox状态改变事件   * 每当CheckBox从选中变成未选中,或者从未选中变为选中均会触发此方法   *   buttonView  代表 当前CheckBox   *   isChecked代表当前CheckBox是否选中状态   */  @Override  public void   onCheckedChanged(CompoundButton   buttonView, boolean   isChecked) {   // TODO Auto-generated method stub     CheckBox cb = (CheckBox) buttonView;     if (isChecked) {//添加到选中项里      strList.add(cb.getText().toString());    Log.i("新选中了:", cb.getText().toString());   } else {//从选中项里移除     strList.remove(cb.getText().toString());    Log.i("取消选中:", cb.getText().toString());   }  } } ******* activity_check_box.xml     xmlns:tools=" http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kn_day04_3_checkbox.CheckBoxActivity" >

             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:textSize="28sp"
        android:text="请选择爱好:" />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/textview1"
        android:layout_marginLeft="60dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="20dp"
        android:textSize="28sp"
        android:text="唱歌"
        />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/cb1"
        android:layout_marginLeft="60dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="20dp"
        android:textSize="28sp"
        android:text="跳舞"
        android:checked="true"
        />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/cb2"
        android:layout_alignLeft="@+id/cb2"
        android:layout_alignStart="@+id/cb2"
        android:layout_marginTop="20dp"
        android:textSize="28sp"
        android:text="运动"
        />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/cb3"
        android:layout_alignLeft="@+id/cb3"
        android:layout_alignStart="@+id/cb3"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="-20dp"
        android:textSize="28sp"
        android:text=" 获取选中项"
        android:focusable="true"
        />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/btn1"
        android:layout_alignLeft="@+id/btn1"
        android:layout_alignStart="@+id/btn1"
        android:layout_marginTop="20dp"
        android:textSize="28sp"
        android:text=" 全选"
        />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/btn2"
        android:layout_alignLeft="@+id/btn2"
        android:layout_alignStart="@+id/btn2"
        android:layout_marginTop="20dp"
        android:textSize="28sp"
        android:text=" 全不选"
        />
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/btn3"
        android:layout_alignLeft="@+id/btn3"
        android:layout_alignStart="@+id/btn3"
        android:layout_marginTop="20dp"
        android:textSize="28sp"
        android:text=" 反选"
        />


更多相关文章

  1. Android P SystemUI下拉时,状态栏和通知栏显示位置不一致。
  2. android SharedPreferences(供选项使用)
  3. Android Studio中隐藏状态栏和标题栏的方法
  4. 修改frameworks源码去掉android的下拉通知状态栏
  5. Android中的状态选择器
  6. Android中状态栏的隐藏
  7. Android Service判断设备联网状态详解
  8. android 设置 button 不同状态的图片

随机推荐

  1. Android增量升级
  2. android stagefright 的 render
  3. android mediaplayer
  4. Android系统(245)---SystemServer进程的创
  5. Android学习 - Android术语解释
  6. Unit Testing in Android
  7. PHP 即将来到 Google Android?
  8. 在Android(安卓)Studio上测试运行,Unity发
  9. Android下使用TinyXml读取xml配置文件(Coc
  10. android自学笔记《一》——android简介