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文件(在附件可下载)因为安 ...
随机推荐
- AngularJS中Route例子
代码:https://files.cnblogs.com/files/xiandedanteng/angularJSRouteSample.rar 点击‘首页’后: 点击‘电脑’后: <!DOC ...
- 将App发布到WasLiberty的较稳妥方法
1.将应用解压放到一个目录 具体步骤: 1.1 建立目录,假设应用包为app.war且和新建目录sp在同一目录下 #mkdir sp 1.2 将app.war 改名为app.zip,这是为了解压#mv ...
- [翻译]MySQL 文档: Control Flow Functions(控制流函数)
本文翻译自13.4 Control Flow Functions Table 13.6 Flow Control Operators 名称 描述 CASE Case 运算符 IF() if/else ...
- 修改Oracle SGA,以提高oracle性能
在正常情况下,查询非常慢. 1.检查SGA大小,以DBA身份连接到oracle数据库,输入show sga. 2.如果SGA过小,请修改其大小 修改SGA必须保持的原则 1).sga_target不能 ...
- 深入解析Windows窗体创建和消息分发
Windows GUI採用基于事件驱动的编程模型,其实差点儿全部的界面库都是这样做的.在纯粹的Window32 SDK编程时代.人们还能够搞懂整个Windows窗口创建和消息的流通过程.可是在如今各种 ...
- PHP计划任务:如何使用Linux的Crontab执行PHP脚本(转)
我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...
- ui-router $transitions 用法
1. //route redirection $transitions.onStart({to: 'manage'}, function (trans) { var params = trans.pa ...
- spring中bean的作用域属性single与prototype的区别
https://blog.csdn.net/linwei_1029/article/details/18408363
- linux 跟踪工具
strace工具,进程诊断.排错.跟踪系统调用和信号量 每行输出都是一个系统调用,包括函数和返回值. strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的 ...
- 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)
...... 承接上一系列动画三. 在主界面后台代码设置嵌套第二个用户控件. using System; using System.Collections.Generic; using System. ...