//MainActivity.Java文件内容
package com.a.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
Button button0;//0数字按钮
Button button1;//1数字按钮
Button button2;//2数字按钮
Button button3;//3数字按钮
Button button4;//4数字按钮
Button button5;//5数字按钮
Button button6;//6数字按钮
Button button7;//7数字按钮
Button button8;//8数字按钮
Button button9;//9数字按钮
Button buttonC;//C清除按钮
Button buttonD;//DEL清除按钮
Button buttonChu;//÷按钮
Button buttonCheng;//×按钮
Button buttonJia;//+按钮
Button buttonJian;//-按钮
Button buttonDeng;//=按钮
Button buttonDian;//小数点按钮
EditText et_input;//显示屏
boolean clear_flag;//清空标识
//以上是初始化控件
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonC = (Button) findViewById(R.id.buttonC);
buttonD = (Button) findViewById(R.id.buttonD);
buttonJia = (Button) findViewById(R.id.buttonJia);
buttonJian = (Button) findViewById(R.id.buttonJian);
buttonCheng = (Button) findViewById(R.id.buttonCheng);
buttonChu = (Button) findViewById(R.id.buttonChu);
buttonDian = (Button) findViewById(R.id.buttonDian);
buttonDeng = (Button) findViewById(R.id.buttonDeng);
et_input = (EditText) findViewById(R.id.et_input);
//以上是实例化控件
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonC.setOnClickListener(this);
buttonD.setOnClickListener(this);
buttonJia.setOnClickListener(this);
buttonJian.setOnClickListener(this);
buttonCheng.setOnClickListener(this);
buttonChu.setOnClickListener(this);
buttonDian.setOnClickListener(this);
buttonDeng.setOnClickListener(this);
//以上是设置监听事件
}
@Override
public void onClick(View v) {
String str = et_input.getText().toString();
switch (v.getId()) {
case R.id.button0:
case R.id.button1:
case R.id.button2:
case R.id.button3:
case R.id.button4:
case R.id.button5:
case R.id.button6:
case R.id.button7:
case R.id.button8:
case R.id.button9:
case R.id.buttonDian:
if (clear_flag) {
str = "";
clear_flag = false;
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;
case R.id.buttonJia:
case R.id.buttonJian:
case R.id.buttonCheng:
case R.id.buttonChu:
if (clear_flag) {
str = "";
clear_flag = false;
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.buttonD:
if (clear_flag) {
clear_flag = false;
str = "";
et_input.setText("");
}else if (str!=null&&!str.equals("")) {
et_input.setText(str.substring(0, str.length()-1));
}
break;
case R.id.buttonC:
clear_flag = false;
str = "";
et_input.setText("");
break;
case R.id.buttonDeng:
getresult();
break;
}
}
private void getresult(){//获取计算结果
String exp = et_input.getText().toString();
if (exp!=null||exp.equals("")) {
return;
}
if (!exp.contains(" ")) {
return;
}
if (clear_flag) {
clear_flag = false;
}
clear_flag = true;
double result=0;
String s1 = exp.substring(0, exp.indexOf(" "));//截取运算符前面的字符
String po = exp.substring(exp.indexOf(" ")+1, exp.indexOf(" ")+2);//截取运算符
String s2 = exp.substring(exp.indexOf(" ")+3);//截取运算符后面的字符
if (!s1.equals("")&&!s2.equals("")) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (po.equals("+")) {
result=d1+d2;
}else if (po.equals("-")) {
result=d1-d2;
}else if (po.equals("×")) {
result=d1*d2;
}else if (po.equals("÷")) {
if (d2==0) {
result=0;
}else {result=d1/d2;}
}
if (!s1.contains(".")&&!s2.contains(".")&&!po.equals("÷")) {
int r = (int) result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}else if (!s1.equals("")&&s2.equals("")) {
et_input.setText(exp);
}else if (s1.equals("")&&!s2.equals("")) {
double d2 = Double.parseDouble(s2);
if (po.equals("+")) {
result=0+d2;
}else if (po.equals("-")) {
result=0-d2;
}else if (po.equals("×")) {
result=0;
}else if (po.equals("÷")) {
result=0;
}
if (!s2.contains(".")) {
int r = (int) result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}else {et_input.setText("");
}
}
}