不论前面计算方式是啥,我按等号后都是输出一个0
package com.example.calculatordemo;
import android.R.integer;
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;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_point;// 小数点
Button btn_divide;// 除以
Button btn_multiply;// 乘以
Button btn_minus;// 减去
Button btn_pluse;// 加
Button btn_equal;// 等于
Button btn_clear;
Button btn_del;
EditText et_input;
boolean needclear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_point = (Button) findViewById(R.id.btn_point);// 小数点
btn_divide = (Button) findViewById(R.id.btn_divide);// 除以
btn_multiply = (Button) findViewById(R.id.btn_multiplay);// 乘以
btn_minus = (Button) findViewById(R.id.btn_minus);// 减去
btn_pluse = (Button) findViewById(R.id.btn_plus);// 加
btn_equal = (Button) findViewById(R.id.btn_equle);// 等于
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
et_input = (EditText) findViewById(R.id.et_input);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_pluse.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = et_input.getText().toString();
switch (v.getId()) {
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_point:
if(needclear&&!str.contains("+")&&!str.contains("-")&&!str.contains("×")&&!str.contains("÷")){
str = "";
et_input.setText("");
needclear = false;
}
et_input.setText(str + ((Button) v).getText());
break;
case R.id.btn_plus:
case R.id.btn_minus:
case R.id.btn_multiplay:
case R.id.btn_divide:
et_input.setText(str +" "+((Button) v).getText()+" ");
break;
case R.id.btn_equle:
getResult();
break;
case R.id.btn_del:
if (str != null && !str.equals("")) {
if(str.substring(str.length()-1,str.length()).equals(" ")){
et_input.setText(str.substring(0, str.length() - 3));
}else{
et_input.setText(str.substring(0, str.length() - 1));
}
}
break;
case R.id.btn_clear:
et_input.setText("");
break;
}
}
/**
* 获取计算结果
*/
private void getResult() {
needclear = true;
boolean pointT = false;
String exp = et_input.getText().toString();
double r = 0;
if(!exp.contains(" ")||exp.substring(exp.length()-1, exp.length()).equals(" ")||exp.contains(" ")){
et_input.setText(exp+"");
Toast.makeText(MainActivity.this, "计算的数据有错", 2).show();
return;
}
String space[] = exp.split(" "); //通过空格获取每个数据
Double arg1 = Double.parseDouble(space[0]);//得到第一个数据,并且转化为Double类型
if(space[0].contains(".")){
pointT = true;
}
for (int i = 1;i<space.length;) {
if(space[i+1].contains(".")){
pointT = true;
}
String op = space[i];
double arg2 = Double.parseDouble(space[i+1]);
if(op.equals("+")){
r = arg1 + arg2;
}else if(op.equals("-")){
r = arg1 - arg2;
}else if(op.equals("×")){
r = arg1 * arg2;
}else if(op.equals("÷")){
if (arg2 == 0)
{
r=0;
}
else
{
r = arg1 / arg2;
}
}
arg1=r;
i=i+2;
}
if(!pointT){
int result = (int)r;
et_input.setText(result+"");
}else{
et_input.setText(r+"");
}
}
}
图形界面我是用表格布局做的,因为不关事吧。。