package com.chaowen;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class Ex04_05_checkBox2 extends Activity {
/** Called when the activity is first created. */
private TextView mTextView1;
private CheckBox mCheckBox1;
private CheckBox mCheckBox2;
private CheckBox mCheckBox3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过findViewById取得TextView对象并调整文字内容
mTextView1=(TextView)findViewById(R.id.myTextView1);
mTextView1.setText("你所选择的项目有:");
//通过findViewById取得三个CheckBox对象
mCheckBox1=(CheckBox)findViewById(R.id.myCheckBox1);
mCheckBox2=(CheckBox)findViewById(R.id.myCheckBox2);
mCheckBox3=(CheckBox)findViewById(R.id.myCheckBox3);
//声明并构造onCheckedChangeListener对象
CheckBox.OnCheckedChangeListener mcheckBoxChanged=new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//通过getString()取得CheckBox的文字字符串
String str0="所选的项目为: ";
String str1=getString(R.string.str_checkbox1);
String str2=getString(R.string.str_checkbox2);
String str3=getString(R.string.str_checkbox3);
String plus=";";
String result="但是超过预算咯!!";
String result2="还可以再多买几本喔!!";
//任一CheckBox被勾选后,该CheckBox的文字会改变TextView的文字内容
if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==true
& mCheckBox3.isChecked()==true)
{
mTextView1.setText(str0+str1+plus+str2+plus+str3+result);
}
else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==true
& mCheckBox3.isChecked()==true)
{
mTextView1.setText(str0+str2+plus+str3+result);
}
else if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==false
& mCheckBox3.isChecked()==true)
{
mTextView1.setText(str0+str1+plus+str3+result);
}
else if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==true
& mCheckBox3.isChecked()==false)
{
mTextView1.setText(str0+str1+plus+str2+result);
}
else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==false
& mCheckBox3.isChecked()==true)
{
mTextView1.setText(str0+str3+plus+result2);
}
else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==true
& mCheckBox3.isChecked()==false)
{
mTextView1.setText(str0+str2);
}
else if(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==false
& mCheckBox3.isChecked()==false)
{
mTextView1.setText(str0+str1);
}
else if(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==false
& mCheckBox3.isChecked()==false)
{
mTextView1.setText(str0);
}
}
};
//设置OnCheckedChangeListener给三个Checkbox对象
mCheckBox1.setOnCheckedChangeListener(mcheckBoxChanged);
mCheckBox2.setOnCheckedChangeListener(mcheckBoxChanged);
mCheckBox3.setOnCheckedChangeListener(mcheckBoxChanged);
}
}
main.xml
<?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/myTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title"
>
</TextView>
<CheckBox
android:id="@+id/myCheckBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_checkbox1"
>
</CheckBox>
<CheckBox
android:id="@+id/myCheckBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_checkbox2"
>
</CheckBox>
<CheckBox
android:id="@+id/myCheckBox3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_checkbox3"
>
</CheckBox>
<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex04_05_checkBox2!</string>
<string name="app_name">Ex04_05_checkBox2</string>
<string name="str_title">我的消費券採購單</string>
<string name="str_checkbox1">泰國機票一張</string>
<string name="str_checkbox2">iPhone一台</string>
<string name="str_checkbox3">Android整合應用範例集一本</string>
</resources>