zoukankan      html  css  js  c++  java
  • Android 使用CheckBox实现多选效果

    CheckBox:复选框
    1.有两种状态:
     选中状态(true),未选中状态(false)
    2.属性:
     android:id="@+id/checkbox"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:checked="false"
     android:text="男"

    CheckBox的默认android:checked属性为false。
    checkBox的OnCheckedChangeListener事件检查勾是否勾选。
    样例程序中有3个CheckBox和1个TextView,TextView事实演示了有多少CheckBox被勾选了以及被勾选的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"
        android:orientation="vertical"
        >
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="篮球" />
    
        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="足球" />
    
        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="乒乓球" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0项选择" />
    
    </LinearLayout>
    activity_main.xml
    package com.example.checkbox;
    
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.TextView;
    
    public class MainActivity extends ActionBarActivity {
        
        private CheckBox basketballCheckBox;
        private CheckBox footballCheckBox;
        private CheckBox pingpongCheckBox;
    
        private boolean[] checkedArray = new boolean[] {false, false, false}; 
        
        private TextView textView;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            basketballCheckBox = (CheckBox) findViewById(R.id.checkBox1);
            footballCheckBox = (CheckBox) findViewById(R.id.checkBox2);
            pingpongCheckBox = (CheckBox) findViewById(R.id.checkBox3);
            textView = (TextView) findViewById(R.id.textView1);
            
            basketballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkedArray[0] = isChecked;
                    textViewResetValue();
                }
            });
            footballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkedArray[1] = isChecked;
                    textViewResetValue();
                }
            });
            pingpongCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkedArray[2] = isChecked;
                    textViewResetValue();
                }
            });
            
        }
    
        private void textViewResetValue() {
            String values = "";
            int sumChecked = 0;
            for (boolean val : checkedArray)
                if (val == true)
                    sumChecked += 1;
            if (sumChecked == 0) 
                textView.setText("0 项选择");
            else {
                if (checkedArray[0] == true) values += ",篮球";
                if (checkedArray[1] == true) values += ",足球";
                if (checkedArray[2] == true) values += ",乒乓球";
                values = sumChecked + "项选择:" + values.substring(1);
                textView.setText(values);
            }
        }
    }
    MainActivity.java

    效果:

    注:Android有一个自己的log记录函数:Log.i()。

  • 相关阅读:
    『Nltk』常用方法
    『Kaggle』分类任务_决策树&集成模型&DataFrame向量化操作
    『Pandas』数据读取&DataFrame切片
    『TensotFlow』RNN中文文本_下_暨研究生开学感想
    『Scrapy』爬取斗鱼主播头像
    『Scrapy』爬取腾讯招聘网站
    『TensorFlow』梯度优化相关
    『Scrapy』终端调用&选择器方法
    『Scrapy』全流程爬虫demo
    使用VS2013、TFS2013和Git进行分布式团队协作
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5402259.html
Copyright © 2011-2022 走看看