Android电话短信拦截项目总结之 多选框(CheckBox)

来源:互联网 发布:淘宝文艺礼物店铺 编辑:程序博客网 时间:2024/05/16 08:48

效果图:


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

1、布局文件:

[html] view plaincopyprint?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <CheckBox  
  12.         android:id="@+id/checkBox1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:layout_marginLeft="40dp"  
  18.         android:layout_marginTop="28dp"  
  19.         android:text="@string/check_java" />  
  20.   
  21.     <CheckBox  
  22.         android:id="@+id/checkBox2"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_alignLeft="@+id/checkBox1"  
  26.         android:layout_below="@+id/checkBox1"  
  27.         android:layout_marginTop="39dp"  
  28.         android:text="@string/check_.net" />  
  29.   
  30.     <CheckBox  
  31.         android:id="@+id/checkBox3"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_alignLeft="@+id/checkBox2"  
  35.         android:layout_below="@+id/checkBox2"  
  36.         android:layout_marginTop="47dp"  
  37.         android:text="@string/check_php" />  
  38.   
  39.     <CheckBox  
  40.         android:id="@+id/checkBox4"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_alignLeft="@+id/checkBox3"  
  44.         android:layout_below="@+id/checkBox3"  
  45.         android:layout_marginTop="40dp"  
  46.         android:text="@string/check_3G" />  
  47.       
  48.      <Button  
  49.         android:id="@+id/button1"  
  50.         android:layout_width="wrap_content"  
  51.         android:layout_height="wrap_content"  
  52.         android:layout_alignRight="@+id/checkBox2"  
  53.         android:layout_below="@+id/checkBox4"  
  54.         android:layout_marginTop="58dp"  
  55.         android:onClick="getValues"  
  56.         android:text="@string/text_get" />  
  57.   
  58. </RelativeLayout>  


 

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

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">lession16-checkbox</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="hello_world">Hello world!</string>  
  7.       
  8.     <string name="check_java">java专业</string>  
  9.     <string name="check_.net">.net专业</string>  
  10.     <string name="check_php">php专业</string>  
  11.     <string name="check_3G">3G专业</string>  
  12.       
  13.     <string name="text_get">获取值</string>  
  14. </resources>  


 

3、MainActivity中的代码:

[java] view plaincopyprint?
  1. package com.example.lession16_checkbox;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.os.Bundle;  
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.widget.CheckBox;  
  12. import android.widget.CompoundButton;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.       
  17.     //声明组件  
  18.     private CheckBox cb1,cb2,cb3,cb4;  
  19.       
  20.     //声明一个集合  
  21.     private List<CheckBox> checkBoxs=new ArrayList<CheckBox>();  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         //获取组件  
  29.         cb1 = (CheckBox) findViewById(R.id.checkBox1);  
  30.         cb2 = (CheckBox) findViewById(R.id.checkBox2);  
  31.         cb3 = (CheckBox) findViewById(R.id.checkBox3);  
  32.         cb4 = (CheckBox) findViewById(R.id.checkBox4);  
  33.           
  34.         //默认选项  
  35.         cb2.setChecked(true);  
  36.         cb4.setChecked(true);  
  37.           
  38.         //注册事件  
  39.         cb1.setOnCheckedChangeListener(listener);  
  40.         cb2.setOnCheckedChangeListener(listener);  
  41.         cb3.setOnCheckedChangeListener(listener);  
  42.         cb4.setOnCheckedChangeListener(listener);  
  43.         //把四个组件添加到集合中去  
  44.         checkBoxs.add(cb1);  
  45.         checkBoxs.add(cb2);  
  46.         checkBoxs.add(cb3);  
  47.         checkBoxs.add(cb4);  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean onCreateOptionsMenu(Menu menu) {  
  52.         // Inflate the menu; this adds items to the action bar if it is present.  
  53.         getMenuInflater().inflate(R.menu.main, menu);  
  54.         return true;  
  55.     }  
  56.       
  57.     public void getValues(View v) {  
  58.   
  59.         String content = "";  
  60.   
  61.         for (CheckBox cbx : checkBoxs) {  
  62.             if (cbx.isChecked()) {  
  63.                 content += cbx.getText() + "\n";  
  64.             }  
  65.         }  
  66.   
  67.         if ("".equals(content)) {  
  68.             content = "您还没有选择尼";  
  69.         }  
  70.         new AlertDialog.Builder(this).setMessage(content).setTitle("选中的内容如下")  
  71.                 .setPositiveButton("关闭"null).show();  
  72.   
  73.     }  
  74.   
  75.     CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {  
  76.   
  77.         @Override  
  78.         public void onCheckedChanged(CompoundButton buttonView,  
  79.                 boolean isChecked) {  
  80.             // TODO Auto-generated method stub  
  81.             CheckBox box = (CheckBox) buttonView;  
  82.   
  83.             Toast.makeText(getApplicationContext(),  
  84.                     "获取的值:" + isChecked + "xxxxx" + box.getText(),  
  85.                     Toast.LENGTH_LONG).show();  
  86.   
  87.         }  
  88.     };  
  89.   
  90. }  

如果没有明白下载源码:
http://download.csdn.net/detail/zhaihaohao1/8616615
0 0