zoukankan      html  css  js  c++  java
  • Android-CheckBox多项选择Demo

    代码

    package com.lxt008;
    
    import com.lxt008.R;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class Activity01 extends Activity
    {
        //用来显示题目
        TextView    m_TextView1;
        //“提交按钮”
        Button        m_Button1;
        //4个多选项
        CheckBox    m_CheckBox1;
        CheckBox    m_CheckBox2;
        CheckBox    m_CheckBox3;
        CheckBox    m_CheckBox4;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            m_TextView1 = (TextView) findViewById(R.id.TextView1);
            m_Button1 = (Button) findViewById(R.id.button1);
    
            /* 取得每个CheckBox对象 */
            m_CheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
            m_CheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
            m_CheckBox3 = (CheckBox) findViewById(R.id.CheckBox3);
            m_CheckBox4 = (CheckBox) findViewById(R.id.CheckBox4);
    
            //对每个选项设置事件监听
            m_CheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if(m_CheckBox1.isChecked())
                    {
                        DisplayToast("你选择了:"+m_CheckBox1.getText());
                    }
                }
            });
            ////////////////////
            m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
                
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if(m_CheckBox2.isChecked())
                    {
                        DisplayToast("你选择了:"+m_CheckBox2.getText());
                    }
                }
            });
            /////////////////
            m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
                
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if(m_CheckBox3.isChecked())
                    {
                        DisplayToast("你选择了:"+m_CheckBox3.getText());
                    }
                }
            });
            ////////////////
            m_CheckBox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
                
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    if(m_CheckBox4.isChecked())
                    {
                        DisplayToast("你选择了:"+m_CheckBox4.getText());
                    }
                }
            });
            //对按钮设置事件监听
            m_Button1.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v)
                {
                    int num = 0;
                    if(m_CheckBox1.isChecked())
                    {
                        num++;
                    }
                    if(m_CheckBox2.isChecked())
                    {
                        num++;
                    }
                    if(m_CheckBox3.isChecked())
                    {
                        num++;
                    }
                    if(m_CheckBox4.isChecked())
                    {
                        num++;
                    }
                    DisplayToast("谢谢参与!你一共选择了"+num+"项!");
                }
            });
        }
        
        /* 显示Toast  */
        public void DisplayToast(String str)
        {
            Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
            //设置toast显示的位置
            toast.setGravity(Gravity.TOP, 0, 220);
            //显示该Toast
            toast.show();
        }
    }

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
          android:id="@+id/TextView1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    <CheckBox
      android:id="@+id/CheckBox1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/CheckBox1"
    >
    </CheckBox>
    <CheckBox
      android:id="@+id/CheckBox2"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/CheckBox2"
    >
    </CheckBox>
    <CheckBox
    android:id="@+id/CheckBox3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/CheckBox3"
    >
    </CheckBox>
    <CheckBox
    android:id="@+id/CheckBox4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/CheckBox4"
    >
    </CheckBox>
        <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="提交"
        >
        </Button>
    </LinearLayout>
  • 相关阅读:
    WPF常用TriggerAction用法 (一)
    一个WPF只能输入数字的行为。
    自定义panel实现,并实现item更改和移除动画。
    MVVM模式下弹出窗体
    ZAM 3D 制作简单的3D字幕 流程(二)
    ZAM 3D 制作简单的3D字幕 流程(一)
    ZAM 3D 制作3D动画字幕 用于Xaml导出
    Metro Win8风格的按钮(Filp翻转)
    WPF自动隐藏的消息框(鼠标放上去将一直显示,移开动画继续),提供normal和error两种边框。
    可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)
  • 原文地址:https://www.cnblogs.com/spadd/p/4189848.html
Copyright © 2011-2022 走看看