当前位置:首页 > Android > 正文内容

安卓对话框之----多选框(CheckBox)

jsc9年前 (2016-04-06)Android3019

简单讲解一下原理:每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。
 CheckBox.setChecked(true);//设置成选中状态。
 CheckBox.getText();//获取多选框的值
 调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传入

1、布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <CheckBox  
        android:id="@+id/checkBox1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="40dp"  
        android:layout_marginTop="28dp"  
        android:text="@string/check_java" />  
  
    <CheckBox  
        android:id="@+id/checkBox2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/checkBox1"  
        android:layout_below="@+id/checkBox1"  
        android:layout_marginTop="39dp"  
        android:text="@string/check_.net" />  
  
    <CheckBox  
        android:id="@+id/checkBox3"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/checkBox2"  
        android:layout_below="@+id/checkBox2"  
        android:layout_marginTop="47dp"  
        android:text="@string/check_php" />  
  
    <CheckBox  
        android:id="@+id/checkBox4"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/checkBox3"  
        android:layout_below="@+id/checkBox3"  
        android:layout_marginTop="40dp"  
        android:text="@string/check_3G" />  
      
     <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignRight="@+id/checkBox2"  
        android:layout_below="@+id/checkBox4"  
        android:layout_marginTop="58dp"  
        android:onClick="getValues"  
        android:text="@string/text_get" />  
  
</RelativeLayout>

2、布局文件中引用的string.xml的值

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  
    <string name="app_name">lession16-checkbox</string>  
    <string name="action_settings">Settings</string>  
    <string name="hello_world">Hello world!</string>  
      
    <string name="check_java">java专业</string>  
    <string name="check_.net">.net专业</string>  
    <string name="check_php">php专业</string>  
    <string name="check_3G">3G专业</string>  
      
    <string name="text_get">获取值</string>  
</resources>

3、MainActivity中的代码:

package com.example.lession16_checkbox;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.app.AlertDialog;  
import android.view.Menu;  
import android.view.View;  
import android.widget.CheckBox;  
import android.widget.CompoundButton;  
import android.widget.Toast;  
  
public class MainActivity extends Activity {  
      
    //声明组件  
    private CheckBox cb1,cb2,cb3,cb4;  
      
    //声明一个集合  
    private List<CheckBox> checkBoxs=new ArrayList<CheckBox>();  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        //获取组件  
        cb1 = (CheckBox) findViewById(R.id.checkBox1);  
        cb2 = (CheckBox) findViewById(R.id.checkBox2);  
        cb3 = (CheckBox) findViewById(R.id.checkBox3);  
        cb4 = (CheckBox) findViewById(R.id.checkBox4);  
          
        //默认选项  
        cb2.setChecked(true);  
        cb4.setChecked(true);  
          
        //注册事件  
        cb1.setOnCheckedChangeListener(listener);  
        cb2.setOnCheckedChangeListener(listener);  
        cb3.setOnCheckedChangeListener(listener);  
        cb4.setOnCheckedChangeListener(listener);  
        //把四个组件添加到集合中去  
        checkBoxs.add(cb1);  
        checkBoxs.add(cb2);  
        checkBoxs.add(cb3);  
        checkBoxs.add(cb4);  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
      
    public void getValues(View v) {  
  
        String content = "";  
  
        for (CheckBox cbx : checkBoxs) {  
            if (cbx.isChecked()) {  
                content += cbx.getText() + "\n";  
            }  
        }  
  
        if ("".equals(content)) {  
            content = "您还没有选择尼";  
        }  
        new AlertDialog.Builder(this).setMessage(content).setTitle("选中的内容如下")  
                .setPositiveButton("关闭", null).show();  
  
    }  
  
    CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {  
  
        @Override  
        public void onCheckedChanged(CompoundButton buttonView,  
                boolean isChecked) {  
            // TODO Auto-generated method stub  
            CheckBox box = (CheckBox) buttonView;  
  
            Toast.makeText(getApplicationContext(),  
                    "获取的值:" + isChecked + "xxxxx" + box.getText(),  
                    Toast.LENGTH_LONG).show();  
  
        }  
    };  
  
}

效果图:

20130625172442765.jpg

扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/72.html

标签: CheckBox
分享给朋友:

“安卓对话框之----多选框(CheckBox) ” 的相关文章

android异步任务详解 AsynTask

android异步任务详解 AsynTask

android提供了一套专门用于异步处理的类。即:AynsTask类。使用这个类可以为耗时程序开辟一个新线程进行处理,处理完时返回。其实,AsynTask类就是对Thread类的一个封装,并且加入了一些新的方法。编程时,两者都可以实现同样的功能。本文后面将对AsynTask和Thread...

Android采用SharedPreferences保存用户登录信息

Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用 SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared...

android上传图片至服务器

本实例实现了android上传手机图片至服务器,服务器进行保存服务器servlet代码publicvoid doPost(HttpServletRequest request, HttpServletResponse response)  ...

Android中的消息通知(NotificationManager和Notification)

下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提 示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。已添加的Notification.Builder,使其更容易构建通知。 notifica...

Location服务之LocationManager

Location服务之LocationManager

LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题,包括查询上一个已知位置、注册和注销来 自某个LocationProvider的周期性的位置更新、注册和注销接近某个坐标时对一个已定义的Intent的触发等。今天我们就一起探讨一下 L...

Android之日期及时间选择对话框

Android之日期及时间选择对话框

清单文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/an...