我的第一个Android程序-计算器
基于Android 2.3.3做的一个练手计算器。
可解析带括号的四则运算。
解析算术表达式的时候,准备调用Webkit通过Js来解析的。
但是2.3.3存在Bug,Js调用Java会导致程序崩溃,
所以没办法,最后是用 BeanShell来解析的。
不过,因为需要每个参与计算的数字都是浮点型,
才能正确无误的保留精度,因为我正则不行,过滤表达式还是花了点功夫
首个Android程序,欢迎大家给与意见和指导~
可解析带括号的四则运算。
解析算术表达式的时候,准备调用Webkit通过Js来解析的。
但是2.3.3存在Bug,Js调用Java会导致程序崩溃,
所以没办法,最后是用 BeanShell来解析的。
不过,因为需要每个参与计算的数字都是浮点型,
才能正确无误的保留精度,因为我正则不行,过滤表达式还是花了点功夫
首个Android程序,欢迎大家给与意见和指导~
标签:
Android SDK
代码片段(4)
[图片] calc.png

[文件] calculator.apk ~ 360KB 下载(137)
[文件] calc-src.zip ~ 2MB 下载(1451)
[代码] [Java]代码
001
|
package com.example.calculator;
|
002
|
003
|
004
|
import java.util.Arrays;
|
005
|
006
|
import bsh.EvalError;
|
007
|
import bsh.Interpreter;
|
008
|
import android.os.Bundle;
|
009
|
import android.app.Activity;
|
010
|
import android.view.View;
|
011
|
import android.view.View.OnClickListener;
|
012
|
import android.widget.Button;
|
013
|
import android.widget.EditText;
|
014
|
015
|
/**
|
016
|
*
@author 铂金小龟
|
017
|
*/
|
018
|
public class CalculatorActivity extends Activity implements OnClickListener{
|
019
|
020
|
EditText
rsText = null ; //显示器
|
021
|
boolean isClear
= false ; //用于是否显示器需要被清理
|
022
|
@Override
|
023
|
protected void onCreate(Bundle
savedInstanceState) {
|
024
|
super .onCreate(savedInstanceState);
|
025
|
setContentView(R.layout.calculator);
|
026
|
|
027
|
//fun
功能按钮
|
028
|
rsText
= (EditText)findViewById(R.id.rsText);
|
029
|
Button
btnDel = (Button)findViewById(R.id.delete);
|
030
|
Button
btnPlu = (Button)findViewById(R.id.plus);
|
031
|
Button
btnMin = (Button)findViewById(R.id.minus);
|
032
|
Button
btnMul = (Button)findViewById(R.id.multiply);
|
033
|
Button
btnDiv = (Button)findViewById(R.id.division);
|
034
|
Button
btnEqu = (Button)findViewById(R.id.equ);
|
035
|
Button
btnTono = (Button)findViewById(R.id.tonone);
|
036
|
Button
btnLeft = (Button)findViewById(R.id.left);
|
037
|
Button
btnRight = (Button)findViewById(R.id.right);
|
038
|
|
039
|
//num
数字按钮
|
040
|
Button
num0 = (Button)findViewById(R.id.num0);
|
041
|
Button
num1 = (Button)findViewById(R.id.num1);
|
042
|
Button
num2 = (Button)findViewById(R.id.num2);
|
043
|
Button
num3 = (Button)findViewById(R.id.num3);
|
044
|
Button
num4 = (Button)findViewById(R.id.num4);
|
045
|
Button
num5 = (Button)findViewById(R.id.num5);
|
046
|
Button
num6 = (Button)findViewById(R.id.num6);
|
047
|
Button
num7 = (Button)findViewById(R.id.num7);
|
048
|
Button
num8 = (Button)findViewById(R.id.num8);
|
049
|
Button
num9 = (Button)findViewById(R.id.num9);
|
050
|
Button
dot = (Button)findViewById(R.id.dot);
|
051
|
|
052
|
//listener
|
053
|
btnTono.setOnClickListener( this );
|
054
|
btnLeft.setOnClickListener( this );
|
055
|
btnRight.setOnClickListener( this );
|
056
|
btnDel.setOnClickListener( this );
|
057
|
btnPlu.setOnClickListener( this );
|
058
|
btnMin.setOnClickListener( this );
|
059
|
btnMul.setOnClickListener( this );
|
060
|
btnDiv.setOnClickListener( this );
|
061
|
btnEqu.setOnClickListener( this );
|
062
|
num0.setOnClickListener( this );
|
063
|
num1.setOnClickListener( this );
|
064
|
num2.setOnClickListener( this );
|
065
|
num3.setOnClickListener( this );
|
066
|
num4.setOnClickListener( this );
|
067
|
num5.setOnClickListener( this );
|
068
|
num6.setOnClickListener( this );
|
069
|
num7.setOnClickListener( this );
|
070
|
num8.setOnClickListener( this );
|
071
|
num9.setOnClickListener( this );
|
072
|
dot.setOnClickListener( this );
|
073
|
}
|
074
|
075
|
@Override
|
076
|
public void onClick(View
e) {
|
077
|
Button
btn = (Button)e;
|
078
|
String
exp = rsText.getText().toString();
|
079
|
if (isClear
&&(
|
080
|
btn.getText().equals( "0" )
|
081
|
||btn.getText().equals( "1" )
|
082
|
||btn.getText().equals( "2" )
|
083
|
||btn.getText().equals( "3" )
|
084
|
||btn.getText().equals( "4" )
|
085
|
||btn.getText().equals( "5" )
|
086
|
||btn.getText().equals( "6" )
|
087
|
||btn.getText().equals( "7" )
|
088
|
||btn.getText().equals( "8" )
|
089
|
||btn.getText().equals( "9" )
|
090
|
||btn.getText().equals( "." ))
|
091
|
||btn.getText().equals( "算数公式错误" )){
|
092
|
rsText.setText( "" );
|
093
|
isClear
= false ;
|
094
|
}
|
095
|
if (btn.getText().equals( "C" )){
|
096
|
rsText.setText( "" );
|
097
|
} else if (btn.getText().equals( "清除" )){
|
098
|
if (isEmpty(exp)) return ;
|
099
|
else
|
100
|
rsText.setText(exp.substring( 0 ,
exp.length()- 1 ));
|
101
|
} else if (btn.getText().equals( "=" )){
|
102
|
if (isEmpty(exp)) return ;
|
103
|
exp
= exp.replaceAll( "×" , "*" );
|
104
|
exp
= exp.replaceAll( "÷" , "/" );
|
105
|
rsText.setText(getRs(exp));
|
106
|
isClear
= false ;
|
107
|
} else {
|
108
|
rsText.setText(rsText.getText()+ "" +btn.getText());
|
109
|
isClear
= false ;
|
110
|
}
|
111
|
//操作完成后始终保持光标在最后一位
|
112
|
rsText.setSelection(rsText.getText().length());
|
113
|
}
|
114
|
115
|
/***
|
116
|
*
@param exp 算数表达式
|
117
|
*
@return 根据表达式返回结果
|
118
|
*/
|
119
|
private String
getRs(String exp){
|
120
|
Interpreter
bsh = new Interpreter();
|
121
|
Number
result = null ;
|
122
|
try {
|
123
|
exp
= filterExp(exp);
|
124
|
result
= (Number)bsh.eval(exp);
|
125
|
} catch (EvalError
e) {
|
126
|
e.printStackTrace();
|
127
|
isClear
= true ;
|
128
|
return "算数公式错误" ;
|
129
|
}
|
130
|
exp
= result.doubleValue()+ "" ;
|
131
|
if (exp.endsWith( ".0" ))
|
132
|
exp
= exp.substring( 0 ,
exp.indexOf( ".0" ));
|
133
|
return exp;
|
134
|
}
|
135
|
136
|
|
137
|
/**
|
138
|
*
因为计算过程中,全程需要有小数参与,所以需要过滤一下
|
139
|
*
@param exp 算数表达式
|
140
|
*
@return
|
141
|
*/
|
142
|
private String
filterExp(String exp) {
|
143
|
String
num[] = exp.split( "" );
|
144
|
String
temp = null ;
|
145
|
int begin= 0 ,end= 0 ;
|
146
|
for ( int i
= 1 ;
i < num.length; i++) {
|
147
|
temp
= num[i];
|
148
|
if (temp.matches( "[+-/()*]" )){
|
149
|
if (temp.equals( "." )) continue ;
|
150
|
end
= i - 1 ;
|
151
|
temp
= exp.substring(begin, end);
|
152
|
if (temp.trim().length()
> 0 &&
temp.indexOf( "." )< 0 )
|
153
|
num[i- 1 ]
= num[i- 1 ]+ ".0" ;
|
154
|
begin
= end + 1 ;
|
155
|
}
|
156
|
}
|
157
|
return Arrays.toString(num).replaceAll( "[\\[\\],
]" , "" );
|
158
|
}
|
159
|
|
160
|
/***
|
161
|
*
@param str
|
162
|
*
@return 字符串非空验证
|
163
|
*/
|
164
|
private boolean isEmpty(String
str){
|
165
|
return (str== null ||
str.trim().length()== 0 );
|
166
|
}
|
167
|
|
168
|
}
|
最后更新:2017-04-03 22:15:47