android实现简单计算器
前台代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"> <TextView
android:id="@+id/whiteblack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:height="150dp"
android:textSize="30sp" /> <TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" > <TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btnclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span=""
android:onClick="onClick"
android:text="CLEAR" /> <Button
android:id="@+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span=""
android:onClick="onClick"
android:text="☜" />
</TableRow> <TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btnchu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="÷" />
</TableRow> <TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btnmul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="×" />
</TableRow> <TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btnsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="-" />
</TableRow> <TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <Button
android:id="@+id/btnpoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="." /> <Button
android:id="@+id/btn_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="" /> <Button
android:id="@+id/btnequ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="=" /> <Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="+" />
</TableRow>
</TableLayout> </LinearLayout>
后台代码如下
package com.example.a19575.jiandanjisuanqi; import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
TextView textView;
Button btnclear;
Button btndelete;
Button btn7;
Button btn8;
Button btn9;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
Button btn0;
Button btnjia;
Button btnjian;
Button btncheng;
Button btnchu;
Button btnpoint;
Button btnequ;
String str1="";
String str2="";
int flagdelete=;
int flagclear=;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.whiteblack);
btnclear=(Button)findViewById(R.id.btnclear);
btndelete=(Button)findViewById(R.id.btndelete);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn9);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn0=(Button)findViewById(R.id.btn_0);
btnjia=(Button)findViewById(R.id.btnadd);
btnjian=(Button)findViewById(R.id.btnsub);
btncheng=(Button)findViewById(R.id.btnmul);
btnchu=(Button)findViewById(R.id.btnchu);
btnpoint=(Button)findViewById(R.id.btnpoint);
btnequ=(Button)findViewById(R.id.btnequ); } public void onClick(View v)
{
switch (v.getId()){
case R.id.btn1:str1+="";break;
case R.id.btn2:str1+="";break;
case R.id.btn3:str1+="";break;
case R.id.btn4:str1+="";break;
case R.id.btn5:str1+="";break;
case R.id.btn6:str1+="";break;
case R.id.btn7:str1+="";break;
case R.id.btn8:str1+="";break;
case R.id.btn9:str1+="";break;
case R.id.btn_0:str1+="";break;
case R.id.btnadd:if(str1.contains("+")==false && str1.contains("-")==false && str1.contains("*")==false && str1.contains("/")==false)str1+="+";break;
case R.id.btnsub:if(str1.contains("+")==false && str1.contains("-")==false && str1.contains("*")==false&& str1.contains("/")==false)str1+="-";break;
case R.id.btnchu:if(str1.contains("+")==false && str1.contains("-")==false && str1.contains("*")==false&& str1.contains("/")==false)str1+="/";break;
case R.id.btnmul:if(str1.contains("+")==false && str1.contains("-")==false && str1.contains("*")==false&& str1.contains("/")==false)str1+="*";break;//保证只含有一个操作数
case R.id.btnpoint:str1+=".";break;
case R.id.btndelete:flagdelete= ;break;
case R.id.btnclear: flagclear=;break;
case R.id.btnequ: str1+="=";break;
}
if(flagdelete==&& str1.length()>=){//删除一个字符
str1=str1.substring(,str1.length()-);
flagdelete=;
}
if(flagclear==){//清空
str1="";
flagclear=;
}
if(str1.length()==&&(str1.charAt()=='+' || str1.charAt()=='-' || str1.charAt()=='*' || str1.charAt()=='/' || str1.charAt()=='.' || str1.charAt()=='=' )){//保证第一个字符只可以是数字
str1="";
}
if(str1.length()>=&&(str1.charAt(str1.length()-)=='-' || str1.charAt(str1.length()-)=='+' || str1.charAt(str1.length()-)=='/' || str1.charAt(str1.length()-)=='*' ||//确保只能输入一个运算符
str1.charAt(str1.length()-)=='=' || str1.charAt(str1.length()-)=='.')&& (str1.charAt(str1.length()-)=='-' ||str1.charAt(str1.length()-)=='+' ||
str1.charAt(str1.length()-)=='*' || str1.charAt(str1.length()-)=='/' || str1.charAt(str1.length()-)=='=' || str1.charAt(str1.length()-)=='.' )){
str1=str1.substring(,str1.length()-);
}
textView.setText(str1);
int t=;
int flag1=;
if(str1.length()>&&str1.charAt(str1.length()-)=='='){
char a[]=str1.toCharArray();
for(int i=;i<str1.length();i++) {
if (a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/') {//得到运算符的位置
t = i;
break;
}
}
double a2;
double b2;
try{
a2 = Double.parseDouble(str1.substring(, t ));
b2 = Double.parseDouble(str1.substring(t + , str1.length() - ));
if (str1.contains("+"))
str2 = String.valueOf(a2 + b2);
if (str1.contains("-"))
str2 = String.valueOf(a2 - b2);
if (str1.contains("*"))
str2 = String.valueOf(a2 * b2);
if (str1.contains("/") && b2 != )
str2 = String.valueOf(a2 / b2);
if (str1.contains("/") && b2 == )
flag1 = ;
}catch (Exception e)
{
textView.setText("error");;
} if(flag1==){
textView.setText("分母不可为零");
flag1=;
}
else
textView.setText(str1+str2);
} }
}
截图如下

总结:该计算器是我暑假随便写的一个app,只实现了两个操作数的运算,功能简单,比较粗糙,实现了对一些常见异常的捕获,第一次写博客,请包涵QAQ。。。。
android实现简单计算器的更多相关文章
- 菜鸟学Android编程——简单计算器《一》
		菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ... 
- 简单计算器 安卓 Android 作业
		Android老师布置的课程作业——简单计算器 功能要求实现四则运算,参考界面见下图: 首先给各位老铁提供apk安装包以及项目压缩包,略表诚意~ github地址:https://github.com ... 
- Android 简单计算器实现源码
		1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ... 
- Android发展简单介绍
		Android一词的本义指“机器人”,同一时候也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,号称是首个为移动 ... 
- 1.C#WinForm基础制作简单计算器
		利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ... 
- Android 实现简单音乐播放器(二)
		在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ... 
- Android 实现简单音乐播放器(一)
		今天掐指一算,学习Android长达近两个月了,今天开始,对过去一段时间的学习收获以及遇到的疑难杂症做一些总结. 简单音乐播放器是我自己完成的第一个功能较为完整的APP,可以说是我的Android学习 ... 
- Android课程---Android Studio简单设置
		Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings-->Appearance-->Theme, ... 
- PAT 06-1 简单计算器
		想看一般简单计算器实现的看客不好意思了,这不是你想要点东西,此处题设为“只能进行加减乘除”.“都是整数”.”优先级相同“和"从左到右".此题来自PAT(http://www.pat ... 
随机推荐
- [IR课程笔记]Web search
			一. 搜索引擎 组成部分: 1. 网络爬虫(web crawler) 2. 索引系统(indexing system) 3. 搜索系统 (searching system) consideratio ... 
- windows下安装Qt
			1.Linux下安装Qt与MySQL相对来说比较容易,在这里我就不多加介绍. 接下来主要介绍windows下安装Qt与MySQL. 2.在windows,我安装QtCreator, 使用的是qt-wi ... 
- DLL中导出ANSI和UNICODE函数
			模仿window中的DLL导出ANSI和UNICODE版本的函数,使用UNICODE宏来控制使用哪个版本: 在函数实际的执行代码UNICODE版本中,在ANSI函数的版本中只做参数的转换,及ANSI字 ... 
- JavaScript:学习笔记(4)——This关键字
			JavaScript:学习笔记(4)——This关键字 以前这篇帖子是关于闭包的,但是我想弄明白的其实是This关键字.JavaScript的this和Java等面向对象语言中的this大不一样,bi ... 
- HDU - 1495 非常可乐  【BFS】
			题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1495 思路 首先 如果可乐的体积 是奇数 那么是无解的 然后 如果能够得到两杯 都是一般容量的可乐 那 ... 
- Matlab图像处理(01)-Matlab基础
			枫竹梦对于Matlab几乎是零基础,只是在上学的时候稍稍接触一点,万万没有想到现在还能用到Matlab.进入正题>>> 图像的基本概念 一幅图像可以被定义为一个二维函数f(x,y), ... 
- SDUT  2766 小明传奇2
			小明传奇2 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 小明不但创建了自己的商店而且选择了建立了自己的货币系统. 传统地,一个 ... 
- elasticsearch _source字段的一些说明
			_source field The _source field contains the original JSON document body that was passed at index ti ... 
- display:inline-bock的注意
			前端当一组元素设置为display:inline-block;时,每个元素之间的回车会被作为一个空格. 
- Go丨语言package github.com/Go-SQL-Driver/MySQL: exec: "git": executable file not found in %PATH%解决方法
			Go语言在添加第三方MySQL驱动的时候报错: go: missing Git command. See https://golang.org/s/gogetcmd package github.co ... 
