package com.example.day16_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 cbx1, cbx2, cbx3, cbx4;
private List<CheckBox> cbxs = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbx1 = (CheckBox) findViewById(R.id.checkBox1);
cbx2 = (CheckBox) findViewById(R.id.checkBox2);
cbx3 = (CheckBox) findViewById(R.id.checkBox3);
cbx4 = (CheckBox) findViewById(R.id.checkBox4);
// 默认选项
cbx1.setChecked(true);
cbx3.setChecked(true);
// 注册事件
/*
* 第一种 cbx1.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
* cbx2.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
* cbx3.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
* cbx4.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
*/
// 第二种
cbx1.setOnCheckedChangeListener(listener);
cbx2.setOnCheckedChangeListener(listener);
cbx3.setOnCheckedChangeListener(listener);
cbx4.setOnCheckedChangeListener(listener);
// 添加到集合中
cbxs.add(cbx1);
cbxs.add(cbx2);
cbxs.add(cbx3);
cbxs.add(cbx4);
cbx1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
}
});
}
public void getValues(View v) {
String content = "";
for (CheckBox cbx : cbxs) {
if (cbx.isChecked()) {
content += cbx.getText() + "\n";
}
}
if ("".equals(content)) {
content = "您还没有选择";
}
new AlertDialog.Builder(this).setMessage(content).setTitle("选中内容如下")
.setPositiveButton("关闭", null).show();
}
// 第一种
/*
* class MyOnCheckedChangeListener implements
* 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();
*
* } }
*/
// 第二种
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();
}
};
}
==================================================================================================
<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_marginTop="41dp"
android:text="@string/text_java" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_centerVertical="true"
android:text="@string/text_php" />
<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="22dp"
android:text="@string/text_3g" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/checkBox4"
android:layout_alignLeft="@+id/checkBox4"
android:layout_marginBottom="17dp"
android:text="@string/text_net" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/checkBox4"
android:layout_below="@+id/checkBox4"
android:onClick="getValues"
android:layout_marginTop="34dp"
android:text="@string/text_get" />
</RelativeLayout>
=================================================================================================
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">day16-checkbox</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="text_java">Java专业</string>
<string name="text_3g">3G专业</string>
<string name="text_net">.Net专业</string>
<string name="text_php">PHP专业</string>
<string name="text_get">查询</string>
</resources>
================================================================================================
效果图: