源码教程 2025年06月7日
0 收藏 0 点赞 602 浏览 4923 个字
摘要 :

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

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

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

Android多选按钮控件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. <TextView android:text="爱好:"

  13. android:textSize="24sp"

  14. android:layout_width="wrap_content"

  15. android:layout_height="wrap_content"

  16. android:id="@+id/tv" />

  17. <RelativeLayout

  18. android:layout_width="wrap_content"

  19. android:layout_height="wrap_content">

  20. <CheckBox

  21. android:layout_width="wrap_content"

  22. android:layout_height="wrap_content"

  23. android:text="音乐"

  24. android:id="@+id/chb_music"

  25. android:layout_alignParentLeft="true"

  26. android:layout_alignParentStart="true"

  27. android:layout_marginTop="10dp"

  28. android:checked="false" />

  29. <CheckBox

  30. android:layout_width="wrap_content"

  31. android:layout_height="wrap_content"

  32. android:text="游戏"

  33. android:id="@+id/chb_game"

  34. android:layout_below="@+id/chb_music"

  35. android:layout_alignParentLeft="true"

  36. android:layout_alignParentStart="true" />

  37. <CheckBox

  38. android:layout_width="wrap_content"

  39. android:layout_height="wrap_content"

  40. android:text="旅游"

  41. android:id="@+id/chb_trip"

  42. android:layout_alignTop="@+id/chb_music"

  43. android:layout_alignRight="@+id/chb_film"

  44. android:layout_alignLeft="@+id/chb_film" />

  45. <CheckBox

  46. android:layout_width="wrap_content"

  47. android:layout_height="wrap_content"

  48. android:text="看电影"

  49. android:id="@+id/chb_film"

  50. android:layout_below="@+id/chb_trip"

  51. android:layout_alignParentRight="true"

  52. android:layout_alignParentEnd="true" />

  53. </RelativeLayout>

  54. <Button

  55. android:id="@+id/end"

  56. android:text="完成"

  57. android:layout_width="wrap_content"

  58. android:layout_height="wrap_content" />

  59. <TextView

  60. android:paddingTop="10dp"

  61. android:id="@+id/result_tv"

  62. android:layout_width="wrap_content"

  63. android:layout_height="wrap_content"

  64. />

  65. </LinearLayout>

当用户对多项选择按钮进行选择时,为了确定用户选择的是哪几项,需要对每个多项选择按钮进行监听。

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

MainActivity 代码如下:

package introduction.android.widgetdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements 
    CompoundButton.OnCheckedChangeListener {
    private CheckBox musicCkb;
    private CheckBox tripCkb;
    private CheckBox filmCkb;
    private CheckBox gameCkb;
    private TextView result_tv;
    private Button endBtn;
    //爱好数组
    ArrayList<String> hobbies=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        musicCkb = (CheckBox) findViewById(R.id.chb_music);
        tripCkb = (CheckBox) findViewById(R.id.chb_trip);
        filmCkb = (CheckBox) findViewById(R.id.chb_film);
        gameCkb = (CheckBox) findViewById(R.id.chb_game);
        result_tv = (TextView) findViewById(R.id.result_tv);
        endBtn= (Button) findViewById(R.id.end);
        //设置监听器
        musicCkb.setOnCheckedChangeListener(this);
        tripCkb.setOnCheckedChangeListener(this);
        filmCkb.setOnCheckedChangeListener(this);
        gameCkb.setOnCheckedChangeListener(this);
        //为button设置监听器
        endBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb=new StringBuilder();
              for (int i =0;i<hobbies.size();i++) {
                  //把选择的爱好添加到string尾部
                  if(i==(hobbies.size()-1))
                  {
                      sb.append(hobbies.get(i));
                  }else {
                      sb.append(hobbies.get(i)+",");
                  }
              }
                //显示选择结果
                result_tv.setText("你选择了:"+sb);
            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            //添加到爱好数组
            hobbies.add(buttonView.getText().toString().trim());
        }else {
            //从数组中移除
            hobbies.remove(buttonView.getText().toString().trim());
        }

    }
}

当 CheckBox 的状态发生改变时,通过 Checkbox.isChecked() 方法可以获取当前 CheckBox 按钮的选中状态,进而进行处理。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/933.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号