89
技術社區[雲棲]
我的第一個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