1. 多选按钮(CheckBox)的基本概念
2. <CheckBox/> 与 CheckBox
3. OnClickListener 与 OnCheckedChangeListener监听器
1. 多选按钮(CheckBox)的基本概念
fragment.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="first.pack.MainActivity$PlaceholderFragment" > 11 12 <CheckBox 13 android:id="@+id/eatId" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="吃饭" /> 17 18 <CheckBox 19 android:id="@+id/sleepId" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="睡觉" /> 23 24 <CheckBox 25 android:id="@+id/dotaId" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:text="dota" /> 29 30 </LinearLayout>
MainActivity.java
1 package first.pack; 2 3 import android.os.Bundle; 4 import android.provider.Settings.System; 5 import android.support.v4.app.Fragment; 6 import android.support.v7.app.ActionBarActivity; 7 import android.util.Log; 8 import android.view.LayoutInflater; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.widget.CheckBox; 14 15 public class MainActivity extends ActionBarActivity { 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 if (savedInstanceState == null) { 23 getSupportFragmentManager().beginTransaction() 24 .add(R.id.container, new PlaceholderFragment()).commit(); 25 } 26 } 27 28 @Override 29 public boolean onCreateOptionsMenu(Menu menu) { 30 31 // Inflate the menu; this adds items to the action bar if it is present. 32 getMenuInflater().inflate(R.menu.main, menu); 33 return true; 34 } 35 36 @Override 37 public boolean onOptionsItemSelected(MenuItem item) { 38 // Handle action bar item clicks here. The action bar will 39 // automatically handle clicks on the Home/Up button, so long 40 // as you specify a parent activity in AndroidManifest.xml. 41 int id = item.getItemId(); 42 if (id == R.id.action_settings) { 43 return true; 44 } 45 return super.onOptionsItemSelected(item); 46 } 47 48 /** 49 * A placeholder fragment containing a simple view. 50 */ 51 public static class PlaceholderFragment extends Fragment { 52 53 private CheckBox eatBox; 54 private CheckBox sleepBox; 55 private CheckBox dotaBox; 56 57 public PlaceholderFragment() { 58 } 59 60 @Override 61 public View onCreateView(LayoutInflater inflater, ViewGroup container, 62 Bundle savedInstanceState) { 63 View rootView = inflater.inflate(R.layout.fragment_main, container, 64 false); 65 66 eatBox = (CheckBox)rootView.findViewById(R.id.eatId); 67 sleepBox = (CheckBox)rootView.findViewById(R.id.sleepId); 68 dotaBox = (CheckBox)rootView.findViewById(R.id.dotaId); 69 70 OnBoxClickListener listener = new OnBoxClickListener(); 71 72 eatBox.setOnClickListener(listener); 73 sleepBox.setOnClickListener(listener); 74 dotaBox.setOnClickListener(listener); 75 76 return rootView; 77 } 78 79 public class OnBoxClickListener implements android.view.View.OnClickListener{ 80 81 @Override 82 public void onClick(View args) { 83 // TODO Auto-generated method stub 84 Log.i("tag","Checkbox is clicked"); //打印语句Log.i(“tag”,"要打印的语句").需要import android.util.Log; 85 } 86 87 } 88 } 89 }
运行之后
勾选checkbox会打印出"checkbox is clicked"
目前问题:1. 用户点击哪一个无法得知
2. 用户 选中 还是 取消 都会打印
关于问题1
1 public class OnBoxClickListener implements android.view.View.OnClickListener{ 2 3 @Override 4 public void onClick(View args) { //参数args即调用onClick的对象,是eatBox或者dotaBox或者sleepBox 5 // TODO Auto-generated method stub 6 Log.i("tag",args.getId()+"");//findViewbyId是根据id得到对象,getId是根据对象得到其id 7 } 8 }
点击红色小叉叉
说明getId()得到的是int类型, 转换为String类型只需在后面加上""
运行结果:按顺序分别点击三个checkbox
打印出来的情况
观察3个id的值可以在gen里面找, 鼠标放在eatId上面, 按F2就可以固定显示的对话框(对话框上面有提示的!!!),就可以截屏了
可以看出可根据 id 判断是点击的哪个 Checkbox
关于问题2
可以使用CheckBox类型的isChecked方法, 当Checkbox被选中则返回真
1 public class OnBoxClickListener implements android.view.View.OnClickListener{ 2 3 @Override 4 public void onClick(View args) { 5 // TODO Auto-generated method stub 6 CheckBox box = (CheckBox)args;//向下转型为CheckBox类型 7 if(box.getId() == R.id.eatId){ 8 if(box.isChecked()){ 9 Log.i("tag","eatBox is Checked"); 10 } 11 else{ 12 Log.i("tag","eatBox is unChecked"); 13 } 14 } 15 else if(box.getId() == R.id.sleepId){ 16 17 } 18 else if(box.getId() == R.id.dotaId){ 19 20 } 21 } 22 }
选中eatBox之后再取消选中, 会打印如下
原文:http://www.cnblogs.com/iMirror/p/3770659.html