基于Android 2.3.3做的一个练手计算器。 
可解析带括号的四则运算。 
解析算术表达式的时候,准备调用Webkit通过Js来解析的。 
但是2.3.3存在Bug,Js调用Java会导致程序崩溃, 
所以没办法,最后是用 BeanShell来解析的。 
不过,因为需要每个参与计算的数字都是浮点型, 
才能正确无误的保留精度,因为我正则不行,过滤表达式还是花了点功夫

脚本代码:

package com.example.calculator;
import java.util.Arrays;
import bsh.EvalError;
import bsh.Interpreter;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author 铂金小龟 
 */
public class CalculatorActivity extends Activity implements OnClickListener{

EditText rsText = null;  //显示器
boolean isClear = false; //用于是否显示器需要被清理
@Override-http://www.huiyi8.com/jiaoben/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);

//fun 功能按钮
rsText = (EditText)findViewById(R.id.rsText);
Button btnDel = (Button)findViewById(R.id.delete);
Button btnPlu = (Button)findViewById(R.id.plus);
Button btnMin = (Button)findViewById(R.id.minus);
Button btnMul = (Button)findViewById(R.id.multiply);
Button btnDiv = (Button)findViewById(R.id.division);
Button btnEqu = (Button)findViewById(R.id.equ);
Button btnTono = (Button)findViewById(R.id.tonone);
Button btnLeft = (Button)findViewById(R.id.left);
Button btnRight = (Button)findViewById(R.id.right);

//num 数字按钮
Button num0 = (Button)findViewById(R.id.num0);
Button num1 = (Button)findViewById(R.id.num1);
Button num2 = (Button)findViewById(R.id.num2);
Button num3 = (Button)findViewById(R.id.num3);
Button num4 = (Button)findViewById(R.id.num4);
Button num5 = (Button)findViewById(R.id.num5);
Button num6 = (Button)findViewById(R.id.num6);
Button num7 = (Button)findViewById(R.id.num7);
Button num8 = (Button)findViewById(R.id.num8);
Button num9 = (Button)findViewById(R.id.num9);
Button dot = (Button)findViewById(R.id.dot);

//listener
btnTono.setOnClickListener(this);
btnLeft.setOnClickListener(this);
btnRight.setOnClickListener(this);
btnDel.setOnClickListener(this);
btnPlu.setOnClickListener(this);
btnMin.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnEqu.setOnClickListener(this);
 num0.setOnClickListener(this);
 num1.setOnClickListener(this);
 num2.setOnClickListener(this);
 num3.setOnClickListener(this);
 num4.setOnClickListener(this);
 num5.setOnClickListener(this);
 num6.setOnClickListener(this);
 num7.setOnClickListener(this);
 num8.setOnClickListener(this);
 num9.setOnClickListener(this);
 dot.setOnClickListener(this);
}

@Override
public void onClick(View e) {
Button btn = (Button)e;
String exp = rsText.getText().toString();
if(isClear &&(
 btn.getText().equals("0")
||btn.getText().equals("1")
||btn.getText().equals("2")
||btn.getText().equals("3")
||btn.getText().equals("4")
||btn.getText().equals("5")
||btn.getText().equals("6")
||btn.getText().equals("7")
||btn.getText().equals("8")
||btn.getText().equals("9")
||btn.getText().equals(".")) 
||btn.getText().equals("算数公式错误")){
rsText.setText("");
isClear = false;
}
if(btn.getText().equals("C")){
rsText.setText("");
}else if(btn.getText().equals("清除")){ 
if(isEmpty(exp)) return;
else
rsText.setText(exp.substring(0, exp.length()-1));
}else if(btn.getText().equals("=")){
if(isEmpty(exp)) return;
exp = exp.replaceAll("×", "*");
exp = exp.replaceAll("÷", "/");
rsText.setText(getRs(exp)); 
isClear = false;
}else{
rsText.setText(rsText.getText()+""+btn.getText());
isClear = false;
}
//操作完成后始终保持光标在最后一位
rsText.setSelection(rsText.getText().length());
}

/***
* @param  exp 算数表达式
* @return 根据表达式返回结果
*/脚本代码
private String getRs(String exp){
Interpreter bsh = new Interpreter();
       Number result = null;
try {
exp = filterExp(exp);
result = (Number)bsh.eval(exp);
} catch (EvalError e) {
e.printStackTrace();
isClear = true;
return "算数公式错误";
}        
exp = result.doubleValue()+"";
if(exp.endsWith(".0"))
exp = exp.substring(0, exp.indexOf(".0"));
return exp;
}

/**
* 因为计算过程中,全程需要有小数参与,所以需要过滤一下
* @param exp 算数表达式
* @return 
*/
private String filterExp(String exp) {
String num[] = exp.split("");
String temp = null;
int begin=0,end=0;
for (int i = 1; i < num.length; i++) {
temp = num[i];
if(temp.matches("[+-/()*]")){
if(temp.equals(".")) continue;
end = i - 1;  
temp = exp.substring(begin, end);
if(temp.trim().length() > 0 && temp.indexOf(".")<0)
num[i-1] = num[i-1]+".0";
begin = end + 1;
}
}
return Arrays.toString(num).replaceAll("[\\[\\], ]", "");
}

/***
* @param str
* @return 字符串非空验证
*/
private boolean isEmpty(String str){
return (str==null || str.trim().length()==0);
}

}

Android程序-计算器的更多相关文章

  1. 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~

    一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...

  2. 正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法

    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法 例如:一个android程序包含两个Activity,分别为MainActivity和Other ...

  3. 怎么让我们自己开发的Android程序设为默认启动

    怎么让我们自己开发的Android程序设为默认启动呢?其实很简单,只要在AndroidManifest.xml文件中配置一下首次启动的那个Activity即要. <activity        ...

  4. Android程序crash处理

    Android程序crash处理 时间 2014-11-24 13:45:37  CSDN博客 原文  http://blog.csdn.net/allen315410/article/details ...

  5. 【Bugly干货分享】手把手教你逆向分析 Android 程序

    很多人写文章,喜欢把什么行业现状啊,研究现状啊什么的写了一大通,感觉好像在写毕业论文似的,我这不废话,先直接上几个图,感受一下. 第一张图是在把代码注入到地图里面,启动首页的时候弹出个浮窗,下载网络的 ...

  6. android开发------第一个android程序

    好吧,现在我们就一起来写第一个android程序,看它带给了我们什么.sdk的使用和虚拟机的创建我就不说了.项目创建过程先略过,不太重要. 那第一个程序我们能学到什么知识呢?一起看吧.^-^ 在IDE ...

  7. 小米手机(HM1SW)高通开发android程序全过程

    小米手机(HM1SW)开发android程序全过程 修改历史: 2016年5月9日  --------  整理文档 a.增加了手机基本信息. b.增加360手机助手连接说明 2016年2月26日  - ...

  8. 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)

    使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB) http://www.cnblogs.com/mrkelly/p/4015245.html 以往调试Androi ...

  9. 使用Visual Studio 2015开发Android 程序

    环境配置: 操作系统:win 7 64位 IDE:Visual Studio 2015 SDK:installer_r24.3.3-windows 安装前提: 编辑hosts文件(在附件可下载)因为安 ...

随机推荐

  1. Solidworks如何绘制螺纹

    1 随便画一个圆柱   2 在原来的地方画一个一摸一样的圆(草图2)   3 在特征选项卡中点击曲线-螺旋线/涡状线   4 设置螺距和圈数,画螺旋线   5 建立一个基准面,第一参考是点,第二参考是 ...

  2. AutoCAD如何快速标注零件序号

    1 先画好一条直线和一个数字   2 选中刚才绘制的数字和直线,选择阵列(估计大概要画四十个就阵列四十行,改一下行偏移)   预览效果如图所示   随后不断重复直线即可   横向也是一样   最后双击 ...

  3. 取消sudo的密码

    终端输入sudo visudo,显示为以下内容: 我们只要修改其中的一点内容,就可以实现sudo不需要输入密码了 sudo su -chmod +w /etc/sudoersvim /etc/sudo ...

  4. linux 木马

    参考 http://wangzan18.blog.51cto.com/8021085/1740113 http://www.cnblogs.com/jluzhsai/p/3756280.html  ( ...

  5. ORACLE数据库导表

    今天在公司的server上面装一个系统,在数据库导表的时候一直导不进去,原先是10g的.dmp文件,导入11g.怀疑版本号不兼容,后来把.dmp表打开,把里面的版本号号改为11g,发现导入还是不行.i ...

  6. 在C#中怎样推断线程当前所处的状态

    在C#中怎样推断线程当前所处的状态 老帅       在C#中.线程对象Thread使用ThreadState属性指示线程状态.它是带Flags特性的枚举类型对象.    ThreadState 为线 ...

  7. 初识Dubbo 系列之6-Dubbo 配置

    配置 Xml配置 配置项说明 具体配置项,请參见:配置參考手冊 (+) API使用说明 假设不想使用Spring配置.而希望通过API的方式进行调用,请參见:API配置 (+) 配置使用说明 想知道怎 ...

  8. webStorm 多列编辑

    webStorm可以像Sublime一样使用列编辑,只是区别在于webStorm只可以编辑连续列表. 按住alt键鼠标选择一列,然后输入文字就会编辑多行,这个功能很赞,比较实用(按住ALT键选中之后, ...

  9. 基于Python的安卓图形锁破解程序

    安卓手机的图形锁是3x3的点阵,按次序连接数个点从而达到锁定/解锁的功能.最少需要连接4个点,最多能连接9个点.网上也有暴力删除手机图形锁的方法,即直接干掉图形锁功能.但假如你想进入别人的手机,但又不 ...

  10. 解决opencv无法读AVI视频的问题

    原文来自:http://blog.csdn.net/yeqiu712/article/details/6220030 其实AVI只是一个外壳.里面的东西可不一样的! 问题:为什么我的电脑支持AVI或者 ...