android 多选按钮CheckBox的使用
2015-02-12 00:00
459 查看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" android:orientation="vertical" tools:context="com.sadhu.s01_e09_checkbox.MainActivity$PlaceholderFragment" > <CheckBox android:id="@+id/eatId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="吃饭" /> <CheckBox android:id="@+id/sleepId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="睡觉" /> <CheckBox android:id="@+id/dotaId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dota" /> <CheckBox android:id="@+id/allCheckedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选"></CheckBox> </LinearLayout>
package com.sadhu.s01_e09_checkbox; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.view.View.OnClickListener; public class MainActivity extends Activity { //定义三个变量 private CheckBox eatBox; private CheckBox sleepBox; private CheckBox dotaBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); //找到三个对象并赋值到相应的变量 eatBox = (CheckBox)findViewById(R.id.eatId); sleepBox = (CheckBox)findViewById(R.id.sleepId); dotaBox = (CheckBox)findViewById(R.id.dotaId); //选中状态发生改变后执行此监听器,两个参数,一个是对象,一个是是否选中。CompoundButton是CheckBox的超类 CheckBoxListener cbListener = new C 7fe0 heckBoxListener(); eatBox.setOnCheckedChangeListener(cbListener); sleepBox.setOnCheckedChangeListener(cbListener); dotaBox.setOnCheckedChangeListener(cbListener); //为全选按钮定义一个改变状态触发的监听器 ((CheckBox)findViewById(R.id.allCheckedBox)).setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) { eatBox.setChecked(isChecked); sleepBox.setChecked(isChecked); dotaBox.setChecked(isChecked); } });; /* //实例化点击事件监听类 OnBoxClickListener onBoxClick = new OnBoxClickListener(); //为三个对象设置点击事件 eatBox.setOnClickListener(onBoxClick); sleepBox.setOnClickListener(onBoxClick); dotaBox.setOnClickListener(onBoxClick);*/ } //定义一个选中状态发生改变就会执行此类 class CheckBoxListener implements OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.getId()==R.id.eatId){ System.out.println("eatBox"); } else if(buttonView.getId()==R.id.sleepId){ System.out.println("sleepBox"); } else if(buttonView.getId()==R.id.dotaId) { System.out.println("dotaBox"); } if(isChecked) { System.out.println("checked"); } else { System.out.println("unchecked"); } } } //定义一个单击事件监听类 /* class OnBoxClickListener implements OnClickListener { @Override public void onClick(View view) { CheckBox box = (CheckBox)view; if(box.getId()==R.id.eatId) { System.out.println("eatBox"); } else if(box.getId()==R.id.sleepId) { System.out.println("sleepBox"); } else if(box.getId()==R.id.dotaId) { System.out.println("dotaBox"); } if(box.isChecked()) { System.out.println("checked"); } else { System.out.println("unchecked"); } } }*/ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } }
多选按钮CheckBox很简单,就是为用户提供多选功能,他有两个监听器,一个是单击(OnClickListener)控件触发的监听器,一个是按钮选中状态发生改变后执行的监听器(OnCheckedChangeListener)。
相关文章推荐
- 从零开始学android<CheckBox多选按钮的使用.八.>
- android中的单选和多选按钮的使用
- 玩转Android---UI篇---CheckBox(多选按钮)
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- 初学Android,笔记2(多选按钮checkbox)
- Android_多选按钮(checkBox) 第一季重制版
- Android单选和多选按钮的使用
- Android小例子:圆角按钮、CheckBox使用、退出确认框、Intent传值、Activity显示软件信息
- Android ListView 使用checkbox 实现 单选、多选操作
- Android入门(5)——多状态按钮ToggleButton、多选按钮CheckBox、RadioGroup与RadioButtont按钮
- Android(5)多选按钮CheckBox、OnClickListener、OnCheckedChangeListener
- Android 使用CheckBox实现好看的开关按钮
- CheckBox 多选按钮的使用方法
- 多选按钮(CheckBox)——Mars Android开发视频教程之第一季第九集(重)
- android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
- Android入门之多选按钮(CheckBox)
- android多选按钮(CheckBox)
- 单选按钮radioButton 多选按钮checkBox Toast 的使用
- PHP 使用checkbox ,select 多选,名称数组的取值和赋值
- Android中使用ListView以及在Listview中添加CheckBox时若干问题