|
今天用到的不是Button而是改用了CheckBox使用的是例用CheckBox的OnCheckedChangeListener做事件触发,最终效果如图:
下面我将一一讲解代码思路:首先从两个文本框开始,先上代码
list=new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(mBox1.isChecked())
{
mBox1.setChecked(false);
}
if(mBox2.isChecked())
{
mBox2.setChecked(false);
}
if(mBox3.isChecked())
{
mBox3.setChecked(false);
}
if(mBox4.isChecked())
{
mBox4.setChecked(false);
}
return false;
}
};
mEditText.setOnKeyListener(list);
mEditText1.setOnKeyListener(list);
上述代码用意在于,当用户之前选过计算方法,想从新输入时,将会激发文本的onkey事件,在事件激发后我们要做的就是如果某一checkbox选中我们将让他失去选中的效果,以便当用户输入完成后可以重新点击计算结果。
功能代码二:
listner=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.Plus:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox1.setChecked(false);
return;
}
break;
case R.id.Cut:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox2.setChecked(false);
return;
}
break;
case R.id.Ride:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox3.setChecked(false);
return;
}
break;
case R.id.Except:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox4.setChecked(false);
return;
}
break;
default:
break;
}
if(mBox1.isChecked())
{
mTextView.setText( GetOperation("+"));
}
else
{
mTextView.setText("");
}
if(mBox2.isChecked())
{
mTextView2.setText(GetOperation("-"));
}
else
{
mTextView2.setText("");
}
if(mBox3.isChecked())
{
mTextView3.setText(GetOperation("*"));
}
else
{
mTextView3.setText("");
}
if(mBox4.isChecked())
{
mTextView4.setText(GetOperation("/"));
}
else
{
mTextView4.setText("");
}
}
};
mBox1.setOnCheckedChangeListener(listner);
mBox2.setOnCheckedChangeListener(listner);
mBox3.setOnCheckedChangeListener(listner);
mBox4.setOnCheckedChangeListener(listner);
如果用户选中将根据选中的某一个checkbox返回计算结果,如果uncheck将清空文本.做这一步之前必须限制两个文本框必须输入,否则程序会崩溃。
全部代码如下:
package cn.terry;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class CheckBoxCalc extends Activity {
private TextView mTextView;
private TextView mTextView2;
private TextView mTextView3;
private TextView mTextView4;
private CheckBox mBox1;
private CheckBox mBox2;
private CheckBox mBox3;
private CheckBox mBox4;
private EditText mEditText;
private EditText mEditText1;
private boolean isbool=true;
private OnCheckedChangeListener listner;
private Float Temp;
private String Experssion;
private OnKeyListener list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView=(TextView)findViewById(R.id.result1);
mTextView2=(TextView)findViewById(R.id.result2);
mTextView3=(TextView)findViewById(R.id.result3);
mTextView4=(TextView)findViewById(R.id.result4);
mBox1=(CheckBox)findViewById(R.id.Plus); //加减乘除
mBox2=(CheckBox)findViewById(R.id.Cut);
mBox3=(CheckBox)findViewById(R.id.Ride);
mBox4=(CheckBox)findViewById(R.id.Except);
mEditText=(EditText)findViewById(R.id.first);
mEditText1=(EditText)findViewById(R.id.second);
list=new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(mBox1.isChecked())
{
mBox1.setChecked(false);
}
if(mBox2.isChecked())
{
mBox2.setChecked(false);
}
if(mBox3.isChecked())
{
mBox3.setChecked(false);
}
if(mBox4.isChecked())
{
mBox4.setChecked(false);
}
return false;
}
};
mEditText.setOnKeyListener(list);
mEditText1.setOnKeyListener(list);
listner=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.Plus:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox1.setChecked(false);
return;
}
break;
case R.id.Cut:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox2.setChecked(false);
return;
}
break;
case R.id.Ride:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox3.setChecked(false);
return;
}
break;
case R.id.Except:
if(!isEmpty(mEditText,mEditText1))
{
Confirm();
mBox4.setChecked(false);
return;
}
break;
default:
break;
}
if(mBox1.isChecked())
{
mTextView.setText( GetOperation("+"));
}
else
{
mTextView.setText("");
}
if(mBox2.isChecked())
{
mTextView2.setText(GetOperation("-"));
}
else
{
mTextView2.setText("");
}
if(mBox3.isChecked())
{
mTextView3.setText(GetOperation("*"));
}
else
{
mTextView3.setText("");
}
if(mBox4.isChecked())
{
mTextView4.setText(GetOperation("/"));
}
else
{
mTextView4.setText("");
}
}
};
mBox1.setOnCheckedChangeListener(listner);
mBox2.setOnCheckedChangeListener(listner);
mBox3.setOnCheckedChangeListener(listner);
mBox4.setOnCheckedChangeListener(listner);
}
public String GetOperation(String Operation)
{
if(Operation=="+")
{
Temp=Float.parseFloat(mEditText.getText().toString())+
Float.parseFloat(mEditText1.getText().toString());
}
if(Operation=="-")
{
Temp=Float.parseFloat(mEditText.getText().toString())-
Float.parseFloat(mEditText1.getText().toString());
}
if(Operation=="*")
{
Temp=Float.parseFloat(mEditText.getText().toString())*
Float.parseFloat(mEditText1.getText().toString());
}
if(Operation=="/")
{
Temp=Float.parseFloat(mEditText.getText().toString())/
Float.parseFloat(mEditText1.getText().toString());
}
Experssion=mEditText.getText().toString()+Operation+mEditText1.getText().toString()
+"="+Temp.toString();
return Experssion;
}
public void Confirm()
{
new AlertDialog.Builder(CheckBoxCalc.this)
.setTitle("提示")
.setMessage("该项不能为空")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create().show();
}
public boolean isEmpty(EditText e,EditText a)
{
if(e.getText().toString().length()>0 && a.getText().toString().length()>0)
{
isbool= true;
}
else
{
isbool=false;
}
return isbool;
}
}
至此功能全部完成,是不是很简单,赶快去试试吧
CheckBoxCalc (1).rar (47.18 KB, 下载次数: 16) |
|