除法的时候总是不能得到小数
package com.example.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{
/*
* 初始化所有按钮
*/
private Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,point;
private Button add,sub,mul,div,equ,clear;
private EditText et;
//清空标识
private boolean clear_flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 实例化所有按钮和组件
*/
btn_0 = (Button) findViewById(R.id.num0);
btn_1 = (Button) findViewById(R.id.num1);
btn_2 = (Button) findViewById(R.id.num2);
btn_3 = (Button) findViewById(R.id.num3);
btn_4 = (Button) findViewById(R.id.num4);
btn_5 = (Button) findViewById(R.id.num5);
btn_6 = (Button) findViewById(R.id.num6);
btn_7 = (Button) findViewById(R.id.num7);
btn_8 = (Button) findViewById(R.id.num8);
btn_9 = (Button) findViewById(R.id.num9);
point = (Button) findViewById(R.id.point);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
equ = (Button) findViewById(R.id.equ);
clear = (Button)findViewById(R.id.clear);
et = (EditText) findViewById(R.id.res); //实例化显示屏
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);
point.setOnClickListener(this);
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
equ.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//获取输入
String str = et.getText().toString();
// 判断当前点击的按钮
switch(v.getId()){
case R.id.num0:
case R.id.num1:
case R.id.num2:
case R.id.num3:
case R.id.num4:
case R.id.num5:
case R.id.num6:
case R.id.num7:
case R.id.num8:
case R.id.num9:
case R.id.point:
if(clear_flag){
clear_flag = false;
str="";
et.setText("");
}
et.setText(str+((Button)v).getText());
break;
case R.id.add:
case R.id.sub:
case R.id.mul:
case R.id.div:
if(clear_flag){
clear_flag = false;
str="";
et.setText("");
}
et.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.clear:
clear_flag=false;
str="";
et.setText("");
case R.id.equ:
getResult();
break;
default:
break;
}
}
/*
* 运算结果
*/
private void getResult() {
//获取输入了的字符
String exp = et.getText().toString();
if(exp==null||exp.equals("")){
return;
}
if(!exp.contains(" ")){
return;
}
if(clear_flag){
clear_flag = false;
return;
}
clear_flag = true;
//初始化得数
double result = 0;
//运算符前面的字符串
String sub = exp.substring(0,exp.indexOf(" "));
//截取的运算符
String ysf = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
//运算符后的字符串
String sub2 = exp.substring(exp.indexOf(" ")+3);
if(!sub.equals("") && !sub2.equals("")){
//初始化两个Double数用来进行计算
double d1 = Double.parseDouble(sub);
double d2 = Double.parseDouble(sub2);
//针对不同的运算符进行判断运算
if(ysf.equals("+")){
result = d1+d2;
}if(ysf.equals("-")){
result = d1-d2;
}if(ysf.equals("x")){
result = d1*d2;
}if(ysf.equals("÷")){
if(d2==0){
result = 0;
} else
result = d1/d2;
}
if(!sub.contains(".") && !sub2.contains(".") && !ysf.equals("÷")){
int r = (int) result;
et.setText(r + "");
} else {
et.setText(result + "");
}
if(!sub.equals("") && sub2.equals("")){
et.setText(exp);
}
if(sub.equals("") && !sub2.equals("")){
if(ysf.equals("+")){
result = 0+d2;
}if(ysf.equals("-")){
result = 0-d2;
}if(ysf.equals("x")){
result = 0;
}if(ysf.equals("÷")){
result = 0;
}
}
if(!sub2.contains(".")){
int r = (int) result;
et.setText(r+"");
} else {
et.setText(result+"");
}
} else {
et.setText("");
}
}
}
有劳老师帮忙看看……