4.5消费券采购列表 —多选项CheckBox的应用
4.5消费券采购列表
—多选项CheckBox的应用
目录
目标:设计三个CheckBox,选中其中的后用TextView显示出选择结果.
方法:对CheckBox设置setOnCheckedChangeListener监听器.
原始代码:
package irdc.EX04_05;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
publicclass EX04_05 extends Activity
{
/*声明对象变量*/
private TextView mTextView1;
private CheckBox mCheckBox1;
private CheckBox mCheckBox2;
private CheckBox mCheckBox3;
/** Called when the activity is first created. */
@Override
publicvoid 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对象*/
mCheckBox1.setOnCheckedChangeListener(mCheckBoxChanged);
mCheckBox2.setOnCheckedChangeListener(mCheckBoxChanged);
mCheckBox3.setOnCheckedChangeListener(mCheckBoxChanged);
}
/*声明并建构onCheckedChangeListener对象*/
private CheckBox.OnCheckedChangeListener mCheckBoxChanged =
new CheckBox.OnCheckedChangeListener()
{
/*implement onCheckedChanged方法*/
@Override
publicvoid onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
/*透过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);
}
elseif(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==true & mCheckBox3.isChecked()==true)
{
mTextView1.setText(str0+str2+plus+str3+result);
}
elseif(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); }
elseif(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==false & mCheckBox3.isChecked()==true)
{
mTextView1.setText(str0+str3+plus+result2);
}
elseif(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==true & mCheckBox3.isChecked()==false)
{
mTextView1.setText(str0+str2);
}
elseif(mCheckBox1.isChecked()==true & mCheckBox2.isChecked()==false & mCheckBox3.isChecked()==false)
{ mTextView1.setText(str0+str1); }
elseif(mCheckBox1.isChecked()==false & mCheckBox2.isChecked()==false & mCheckBox3.isChecked()==false)
{ mTextView1.setText(str0);
}
}
};
}
改进思路:
对各种状况进行编码,使用switch语句,使结构清晰易懂,易于维护.
/*对所有情况进行编码
* 编码表
* 权值 100 10 1 /
* flag1 flag2 falg3 total
* 0 0 0 0
* 0 0 1 1
* 0 1 0 10
* 0 1 1 11
* 1 0 0 100
* 1 0 1 101
* 1 1 0 110
* 1 1 1 111
* */
改进代码:
(使用条件表达式替代if else赋值.)
package edu.cquptzx.MutiCheckBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
publicclass MutiCheckBoxActivity extends Activity {
private TextView tv;
private CheckBox cb1,cb2,cb3;
private String str1,str2,str3 ,str_BR,str_NULLBILL;
privateintflag1 = 0,flag2 = 0,flag3 = 0 , total = 0 ;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据ID找到对象;
tv = (TextView)findViewById(R.id.listBill);
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
cb3 = (CheckBox) findViewById(R.id.checkBox3);
str1 = getString(R.string.item_1);
str2 = getString(R.string.item_2);
str3 = getString(R.string.item_3);
str_BR = getString(R.string.str_BR);
str_NULLBILL = getString(R.string.str_NULLBILL);
tv.setText(str_NULLBILL);
CheckBox.OnCheckedChangeListener cblistener = new CheckBox.OnCheckedChangeListener ()
{
publicvoid onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
/*对所有情况进行编码
* 编码表
* 权值 100 10 1 /
* flag1 flag2 falg3 total
* 0 0 0 0
* 0 0 1 1
* 0 1 0 10
* 0 1 1 11
* 1 0 0 100
* 1 0 1 101
* 1 1 0 110
* 1 1 1 111
* */
flag1 = (cb1.isChecked()==true) ? 100 : 0;
flag2 = (cb2.isChecked()==true) ? 10 : 0;
flag3 = (cb3.isChecked()==true) ? 1 : 0;
/*
if(cb1.isChecked())
{
flag1 = 100;
}
else
{
flag1 = 0;
}
if(cb2.isChecked())
{
flag2 = 10;
}
else
{
flag2 = 0;
}
if(cb3.isChecked())
{
flag3 = 1 ;
}
else
{
flag3 = 0;
}
*/
total = flag1 + flag2 + flag3 ;
switch(total)
{
case 0 :
tv.setText(str_NULLBILL);
break;
case 1:
tv.setText(str3);
break;
case 10 :
tv.setText(str2);
break;
case 11 :
tv.setText(str2 + str_BR + str3);
break;
case 100:
tv.setText(str1);
break;
case 101:
tv.setText(str1 + str_BR + str3);
break;
case 110:
tv.setText(str1 + str_BR + str2);
break;
case 111:
tv.setText(str1 + str_BR + str2 + str_BR + str3);
break;
default:
break;
}
}
};
cb1.setOnCheckedChangeListener(cblistener);
cb2.setOnCheckedChangeListener(cblistener);
cb3.setOnCheckedChangeListener(cblistener);
}
}
效果:
For more questions , contacts me by :
cquptzx@qq.com or cquptzx@outlook.com