Android程序-计算器
基于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程序-计算器的更多相关文章
- 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~
一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...
- 正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法 例如:一个android程序包含两个Activity,分别为MainActivity和Other ...
- 怎么让我们自己开发的Android程序设为默认启动
怎么让我们自己开发的Android程序设为默认启动呢?其实很简单,只要在AndroidManifest.xml文件中配置一下首次启动的那个Activity即要. <activity ...
- Android程序crash处理
Android程序crash处理 时间 2014-11-24 13:45:37 CSDN博客 原文 http://blog.csdn.net/allen315410/article/details ...
- 【Bugly干货分享】手把手教你逆向分析 Android 程序
很多人写文章,喜欢把什么行业现状啊,研究现状啊什么的写了一大通,感觉好像在写毕业论文似的,我这不废话,先直接上几个图,感受一下. 第一张图是在把代码注入到地图里面,启动首页的时候弹出个浮窗,下载网络的 ...
- android开发------第一个android程序
好吧,现在我们就一起来写第一个android程序,看它带给了我们什么.sdk的使用和虚拟机的创建我就不说了.项目创建过程先略过,不太重要. 那第一个程序我们能学到什么知识呢?一起看吧.^-^ 在IDE ...
- 小米手机(HM1SW)高通开发android程序全过程
小米手机(HM1SW)开发android程序全过程 修改历史: 2016年5月9日 -------- 整理文档 a.增加了手机基本信息. b.增加360手机助手连接说明 2016年2月26日 - ...
- 使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)
使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB) http://www.cnblogs.com/mrkelly/p/4015245.html 以往调试Androi ...
- 使用Visual Studio 2015开发Android 程序
环境配置: 操作系统:win 7 64位 IDE:Visual Studio 2015 SDK:installer_r24.3.3-windows 安装前提: 编辑hosts文件(在附件可下载)因为安 ...
随机推荐
- 转载:JAVA中获取项目文件路径
本文转载自:http://blog.163.com/michaelgaoit%40126/blog/static/11389538620103711613620/ web 上运行 1:this.get ...
- jsp中jquery用法一步刷新 验证用户名是否存在
<script type="text/javascript"> /* $(document).ready(function(){ var id="ha&quo ...
- Log4net日志记录、详细配置(自己使用>)
log4net库是Apache log4j框架在Microsoft.NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具 1.首先添加对log4net.dll的引 ...
- AWS上的游戏服务:Lumberyard + Amazon GameLift + Twitch
开发一款世界级的游戏是一个非常困难,耗时和昂贵的过程.如今的游戏玩家要求越来越苛刻,他们希望既能够通过各种不同的终端设备来进行游戏 ,又要求游戏具有社交的功能. 因为此类游戏的开发期和推广期都非常长. ...
- 时钟展频技术能有效降低EMI,深入讲解展频发生器!
原文地址:https://baijiahao.baidu.com/s?id=1608649367453023659&wfr=spider&for=pc 相关文章: 1.http://b ...
- ordinal parameter mismatch
© 版权声明:本文为博主原创文章,转载请注明出处 错误描述:Caused by: org.hibernate.HibernateException: ordinal parameter mismatc ...
- POJ 2253 Frogger(最小最大距离)
题意 给你n个点的坐标 求第1个点到第2个点的全部路径中两点间最大距离的最小值 非常水的floyd咯 #include<cstdio> #include<cmath> #i ...
- 搜狐新闻APP是如何使用HUAWEI DevEco IDE快速集成HUAWEI HiAI Engine
6月12日,搜狐新闻APP最新版本在华为应用市场正式上线啦! 那么,这一版本的搜狐新闻APP有什么亮点呢? 先抛个图,来直接感受下—— 模糊图片,瞬间清晰! 效果杠杠的吧. 而藏在这项神操作背后的 ...
- git系列1
git clone支持多种协议,除了HTTP(s)以外,还支持SSH.Git.本地文件协议等,下面是一些例子. $ git clone http[s]://example.com/path/to/re ...
- 图像处理之canny---求梯度
梯度求法和sobel之类的算子雷同,甚至更简单,就是一个离散差分,不清楚的童鞋可以百度,一大堆资料呢,从源码也可清晰的看出原理. // 方向导数,求梯度/* * @parameter sz: 图像大小 ...