橘子味的心
标题:4.6 Android多选按钮

多项选择按钮(CheckBox)属于输入型组件,该组件允许用户一次选择多个选项。当用户不方便在手机屏幕上直接进行输入操作时,该组件的使用显得尤为方便。

下面通过实例讲解 CheckBox 的使用方法。该实例的运行效果如图 1 所示。

CheckBox的应用界面
图 1  CheckBox 的应用界面

工程 WidgetDemo 中的布局文件 main.xml 中增加的代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:paddingRight="10dp"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:orientation="vertical"
  10. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  11. tools:context=".MainActivity">
  12.  
  13. <TextView android:text="爱好:"
  14. android:textSize="24sp"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:id="@+id/tv" />
  18.  
  19. <RelativeLayout
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content">
  22. <CheckBox
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="音乐"
  26. android:id="@+id/chb_music"
  27. android:layout_alignParentLeft="true"
  28. android:layout_alignParentStart="true"
  29. android:layout_marginTop="10dp"
  30. android:checked="false" />
  31.  
  32. <CheckBox
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="游戏"
  36. android:id="@+id/chb_game"
  37. android:layout_below="@+id/chb_music"
  38. android:layout_alignParentLeft="true"
  39. android:layout_alignParentStart="true" />
  40.  
  41. <CheckBox
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="旅游"
  45. android:id="@+id/chb_trip"
  46. android:layout_alignTop="@+id/chb_music"
  47. android:layout_alignRight="@+id/chb_film"
  48. android:layout_alignLeft="@+id/chb_film" />
  49.  
  50. <CheckBox
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:text="看电影"
  54. android:id="@+id/chb_film"
  55. android:layout_below="@+id/chb_trip"
  56. android:layout_alignParentRight="true"
  57. android:layout_alignParentEnd="true" />
  58.  
  59. </RelativeLayout>
  60. <Button
  61. android:id="@+id/end"
  62. android:text="完成"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content" />
  65.  
  66. <TextView
  67. android:paddingTop="10dp"
  68. android:id="@+id/result_tv"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. />
  72. </LinearLayout>
当用户对多项选择按钮进行选择时,为了确定用户选择的是哪几项,需要对每个多项选择按钮进行监听。

CompoundButton.OnCheckedChangedListener 接口可用于对 CheckBox 的状态进行监听。当 CheckBox 的状态在未被选中和被选中之间变化时,该接口的 onCheckedChanged() 方法会被系统调用。CheckBox 通过 setOnCheckedChangeListener() 方法将该接口对象设置为自己的监听器。

MainActivity 代码如下:
  1. package introduction.android.widgetdemo;
  2.  
  3. import android.os.Bundle;
  4. import android.support.design.widget.FloatingActionButton;
  5. import android.support.design.widget.Snackbar;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.support.v7.widget.Toolbar;
  8. import android.view.View;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.widget.Button;
  12. import android.widget.CheckBox;
  13. import android.widget.CompoundButton;
  14. import android.widget.TextView;
  15. import java.util.ArrayList;
  16.  
  17. public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
  18. private CheckBox musicCkb;
  19. private CheckBox tripCkb;
  20. private CheckBox filmCkb;
  21. private CheckBox gameCkb;
  22. private TextView result_tv;
  23. private Button endBtn;
  24. //爱好数组
  25. ArrayList<String> hobbies=new ArrayList<String>();
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31. //初始化控件
  32. musicCkb = (CheckBox) findViewById(R.id.chb_music);
  33. tripCkb = (CheckBox) findViewById(R.id.chb_trip);
  34. filmCkb = (CheckBox) findViewById(R.id.chb_film);
  35. gameCkb = (CheckBox) findViewById(R.id.chb_game);
  36. result_tv = (TextView) findViewById(R.id.result_tv);
  37. endBtn= (Button) findViewById(R.id.end);
  38. //设置监听器
  39. musicCkb.setOnCheckedChangeListener(this);
  40. tripCkb.setOnCheckedChangeListener(this);
  41. filmCkb.setOnCheckedChangeListener(this);
  42. gameCkb.setOnCheckedChangeListener(this);
  43. //为button设置监听器
  44. endBtn.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. StringBuilder sb=new StringBuilder();
  48. for (int i =0;i<hobbies.size();i++) {
  49. //把选择的爱好添加到string尾部
  50. if(i==(hobbies.size()-1))
  51. {
  52. sb.append(hobbies.get(i));
  53. }else {
  54. sb.append(hobbies.get(i)+",");
  55. }
  56. }
  57. //显示选择结果
  58. result_tv.setText("你选择了:"+sb);
  59. }
  60. });
  61. }
  62.  
  63. @Override
  64. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  65. if (isChecked){
  66. //添加到爱好数组
  67. hobbies.add(buttonView.getText().toString().trim());
  68. }else {
  69. //从数组中移除
  70. hobbies.remove(buttonView.getText().toString().trim());
  71. }
  72.  
  73. }
  74. }
当 CheckBox 的状态发生改变时,通过 Checkbox.isChecked() 方法可以获取当前 CheckBox 按钮的选中状态,进而进行处理。