Android电话短信拦截项目总结之 多选框(CheckBox)
2015-04-21 15:36
585 查看
效果图:
简单讲解一下原理:每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。
CheckBox.setChecked(true);//设置成选中状态。
CheckBox.getText();//获取多选框的值
调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传入
1、布局文件:
[html] view
plaincopyprint?
<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的值
[html] view
plaincopyprint?
<?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中的代码:
[java] view
plaincopyprint?
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();
}
};
}
如果没有明白下载源码:
http://download.csdn.net/detail/zhaihaohao1/8616615
简单讲解一下原理:每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。
CheckBox.setChecked(true);//设置成选中状态。
CheckBox.getText();//获取多选框的值
调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传入
1、布局文件:
[html] view
plaincopyprint?
<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的值
[html] view
plaincopyprint?
<?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中的代码:
[java] view
plaincopyprint?
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();
}
};
}
如果没有明白下载源码:
http://download.csdn.net/detail/zhaihaohao1/8616615
相关文章推荐
- Android电话短信拦截项目总结之 checkbox和listview抢焦点
- Android电话短信拦截项目总结之 SQLite数据库简介和使用
- Android电话短信拦截项目总结之 项目中sqlite运用
- Android电话短信拦截项目总结之 项目中线性布局比重使用
- Android电话短信拦截项目总结之 单选框(RadioButton)的使用方法
- Android电话短信拦截项目总结之 开发工具
- Android电话短信拦截项目总结之 项目源码及相关学习资料
- Android项目:手机安全卫士(12)—— 通讯卫士之电话短信黑名单设置与拦截
- Android项目:手机安全卫士(12)—— 通讯卫士之电话短信黑名单设置与拦截
- Android开发——短信电话拦截/接听电话
- Android开发——短信电话拦截/接听电话
- Android-实现对指定电话的短信拦截和来电拦截
- 这是一个用于拦截android实时短信的库,可以进行短信过滤,得到自己想要的内容,可以用于需要自动填写短信验证码的app项目
- Android项目实现短信的发送、接收和对短信进行拦截
- Android小项目之--电话与短信服务
- Android项目:手机安全卫士(13)—— 通讯卫士之电话拦截与挂断
- day5.10总结_短信和电话拦截
- Android安卓电话拦截及短信过滤
- Android短信拦截和电话拦截
- android143 360 短信电话拦截