多选按钮(CheckBox)

今天我们介绍的是Checkbox多选框:

1.Activity

//复选框,[基础控件]---状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用
public class CheckBoxActivity extends Activity implements CompoundButton.OnCheckedChangeListener{

    private Context context;
    private CheckBox sleepCheckBox;
    private CheckBox dadoudouCheckBox;
    private CheckBox gameCheckBox;
    private CheckBox shoppingCheckBox;
    private CheckBox filmCheckBox;
    private CheckBox sportCheckBox;
    private Button submitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_box);

        init();
        addAction();

    }

    private void init(){
        context = this;
        sleepCheckBox = (CheckBox)findViewById(R.id.sleepCheckBoxId);
        dadoudouCheckBox = (CheckBox)findViewById(R.id.dadoudouCheckBoxId);
        gameCheckBox = (CheckBox)findViewById(R.id.gameCheckBoxId);
        shoppingCheckBox = (CheckBox)findViewById(R.id.shoppingCheckBoxId);
        filmCheckBox = (CheckBox)findViewById(R.id.filmCheckBoxId);
        sportCheckBox = (CheckBox)findViewById(R.id.sportCheckBoxId);
        submitButton = (Button)findViewById(R.id.submitButtonId);
    }

    private void addAction(){
        sleepCheckBox.setOnCheckedChangeListener(this);
        dadoudouCheckBox.setOnCheckedChangeListener(this);
        gameCheckBox.setOnCheckedChangeListener(this);
        shoppingCheckBox.setOnCheckedChangeListener(this);
        filmCheckBox.setOnCheckedChangeListener(this);
        sportCheckBox.setOnCheckedChangeListener(this);
        submitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //String 字符串常量
                //StringBuffer 字符串变量(线程安全)
                //StringBuilder 字符串变量(非线程安全)
                StringBuilder sb = new StringBuilder("您的兴趣是:");
                //MyStringBuilder.Insert(6,"Beautiful ");
                //MyStringBuilder.Remove(5,7);
                //MyStringBuilder.Replace(‘!‘, ‘?‘);
                //代码示例指定可以将 MyStringBuilder对象扩充到最大 25个空白。
                //StringBuilderMyStringBuilder = new StringBuilder("Hello World!", 25);
                if(sleepCheckBox.isChecked()){
                    sb.append("睡觉 ");
                }
                if(dadoudouCheckBox.isChecked()){
                    sb.append("打豆豆 ");
                }
                if(gameCheckBox.isChecked()){
                    sb.append("游戏 ");
                }
                if(shoppingCheckBox.isChecked()){
                    sb.append("购物 ");
                }
                if(filmCheckBox.isChecked()){
                    sb.append("电影 ");
                }
                if(sportCheckBox.isChecked()){
                    sb.append("运动");
                }

                Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        switch(id){
        case R.id.sleepCheckBoxId:
            if(isChecked){
                Toast.makeText(context, "你选择了\"睡觉\"", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "你取消选择了\"睡觉\"", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.dadoudouCheckBoxId:
            if(isChecked){
                Toast.makeText(context, "你选择了\"打豆豆\"", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "你取消选择了\"打豆豆\"", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.gameCheckBoxId:
            if(isChecked){
                Toast.makeText(context, "你选择了\"游戏\"", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "你取消选择了\"游戏\"", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.shoppingCheckBoxId:
            if(isChecked){
                Toast.makeText(context, "你选择了\"购物\"", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "你取消选择了\"购物\"", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.filmCheckBoxId:
            if(isChecked){
                Toast.makeText(context, "你选择了\"电影\"", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "你取消选择了\"电影\"", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.sportCheckBoxId:
            if(isChecked){
                Toast.makeText(context, "你选择了\"运动\"", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(context, "你取消选择了\"运动\"", Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }

}

2.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 复选框页面 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="兴趣:"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/sleepCheckBoxId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="睡觉" />

        <CheckBox
            android:id="@+id/dadoudouCheckBoxId"
            android:layout_width="85dp"
            android:layout_height="wrap_content"
            android:text="打豆豆" />

        <CheckBox
            android:id="@+id/gameCheckBoxId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="游戏" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/shoppingCheckBoxId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="购物" />

        <CheckBox
            android:id="@+id/filmCheckBoxId"
            android:layout_width="85dp"
            android:layout_height="wrap_content"
            android:text="电影" />

        <CheckBox
            android:id="@+id/sportCheckBoxId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="运动" />
    </LinearLayout>

    <Button
        android:id="@+id/submitButtonId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="确定"
        android:textSize="20sp" />

</LinearLayout>

3.效果图如下:

时间: 2025-01-14 20:19:56

多选按钮(CheckBox)的相关文章

android 多选按钮CheckBox的使用

<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"     and

zepto全选按钮之全选会根据按钮是否被全部选中更改状态

在做手机端二次开发购物车的时候,发现zepto全选,没找到,或者功能不是自己想要的 后来做好,分享给需要的人 //全选或多选处理      var CheckAll = $('#items_check_all');    var checkbox = $('input[name^="check"]');    var removeUrl = '<{link app=b2c ctl=wap_cart act=remove}>';        //初始化,把所有选中的加上状态

checkbox做全选按钮

1.先写一个html页面,里面写一个全选按钮和几个复选框,实现下面2个要求 (1)点击全选按钮选中时,所有的复选框选中. (2)点击全选按钮取消选中时,所有复选框取消选中. <input type="checkbox" id="quanxuan" />全选<br /> <input type="checkbox" class="qx" />aa<br /> <input t

安卓开发_复选按钮控件(CheckBox)的简单使用

复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈 复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性 一.由于复选框可以选中多项,所有为了确定用户是否选择了某一项,还需要为每一个选项添加setOnCheckedChangeListener事件监听 例如: 为id为like1的复选按钮添加状态改变事件监听,代码如下 1 final CheckBox like1 = (CheckBox)findViewById

js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false

用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种方式使<input type="checkbox" />中的复选框被选中. 方法一:直接在HTML行间中添加checked属性.   eg:<input type="checkbox" checked /> 方法二:使用javascript使in

android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发

android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发 本博文主要讲述的是android开发中的单选和多选按钮的使用,具体情况请看实例代码: MainActivity.java: package com.example.radiotest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.CheckBox; imp

html,javaScript中怎么控制复选框checkbox的全选,全不选,以及全选中,全选按钮选中,其中一个或者多个没选,则全选按钮不被选中

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ include file="/commons/jsp/htmtag.jsp"%> <html><head> <meta http-equiv="Content-Type"

checkbox 全选/反选,勾掉一个子节点全选按钮为空的demo

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@taglib uri="http://java.sun.com/

UI控件之RadioButton(单选按钮)&amp;Checkbox(复选按钮)

(一)概述: (二)RadioButton的基本用法与事件处理: 效果图: 实现代码: xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:

多选框checkbox全选全不选和反选

在判断多选框中的某一个是否被选中时,我们可以用checked的属性 选中了就返回true,没被选中就返回false; 这是html代码: <form action="#"> <label for="hobby">爱好:</label> <label for="hobby1"> <input type="checkbox" name="hobby" id=