Android实现简单的计算器功能

**前言:**通过Android实现简单的计算器功能,实现简单的加、减、乘、除操作。

效果图如下:

  1. 第一步

布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="10dp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="10dp"tools:context="com.newdegree.calculator.MainActivity"><EditTextandroid:id="@+id/et_input"android:layout_width="fill_parent"android:layout_height="60dp"android:inputType="none"android:gravity="center|right"android:background="#f0f0f0"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_clear"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_del"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="DEL"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_divide"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="/"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_multply"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="*"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="7"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="8"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="9"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_minus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="-"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="4"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="5"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="6"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_plus"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="+"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="2"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="3"android:textSize="20sp"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="10dp"android:weightSum="1"><Buttonandroid:id="@+id/btn_0"android:layout_width="180dp"android:layout_height="wrap_content"android:text="0"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_point"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_weight="18.10"android:text="."android:textSize="20sp"/></LinearLayout></LinearLayout><Buttonandroid:id="@+id/btn_equal"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginLeft="5dp"android:background="@android:color/holo_orange_light"android:text="="android:textSize="20sp"/></LinearLayout></LinearLayout>

布局文件中定义了计算器界面的一些按钮和显示数字的组件。

  1. 第二步

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button btn_0;//0数字按钮private Button btn_1;//1数字按钮private Button btn_2;//2数字按钮private Button btn_3;//3数字按钮private Button btn_4;//4数字按钮private Button btn_5;//5数字按钮private Button btn_6;//6数字按钮private Button btn_7;//7数字按钮private Button btn_8;//8数字按钮private Button btn_9;//9数字按钮private Button btn_point;//小数点按钮private Button btn_clear;//clear按钮private Button btn_del;//del按钮private Button btn_plus;//+按钮private Button btn_minus;//-按钮private Button btn_multply;//*按钮private Button btn_divide;//除号按钮private Button btn_equal;//=按钮private EditText editText;boolean clear_flag;//清空标识@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_0 = (Button) findViewById(R.id.btn_0);btn_1 = (Button) findViewById(R.id.btn_1);btn_2 = (Button) findViewById(R.id.btn_2);btn_3 = (Button) findViewById(R.id.btn_3);btn_4 = (Button) findViewById(R.id.btn_4);btn_5 = (Button) findViewById(R.id.btn_5);btn_6 = (Button) findViewById(R.id.btn_6);btn_7 = (Button) findViewById(R.id.btn_7);btn_8 = (Button) findViewById(R.id.btn_8);btn_9 = (Button) findViewById(R.id.btn_9);btn_point = (Button) findViewById(R.id.btn_point);btn_clear = (Button) findViewById(R.id.btn_clear);btn_del = (Button) findViewById(R.id.btn_del);btn_plus = (Button) findViewById(R.id.btn_plus);btn_minus = (Button) findViewById(R.id.btn_minus);btn_multply = (Button) findViewById(R.id.btn_multply);btn_divide = (Button) findViewById(R.id.btn_divide);btn_equal = (Button) findViewById(R.id.btn_equal);editText = (EditText) findViewById(R.id.et_input);btn_0.setOnClickListener(this);btn_1.setOnClickListener(this);btn_2.setOnClickListener(this);btn_3.setOnClickListener(this);btn_4.setOnClickListener(this);btn_5.setOnClickListener(this);btn_6.setOnClickListener(this);btn_7.setOnClickListener(this);btn_8.setOnClickListener(this);btn_9.setOnClickListener(this);btn_point.setOnClickListener(this);btn_clear.setOnClickListener(this);btn_del.setOnClickListener(this);btn_plus.setOnClickListener(this);btn_minus.setOnClickListener(this);btn_multply.setOnClickListener(this);btn_divide.setOnClickListener(this);btn_equal.setOnClickListener(this);}@Overridepublic void onClick(View view) {String input = editText.getText().toString();switch (view.getId()){case R.id.btn_0:case R.id.btn_1:case R.id.btn_2:case R.id.btn_3:case R.id.btn_4:case R.id.btn_5:case R.id.btn_6:case R.id.btn_7:case R.id.btn_8:case R.id.btn_9:case R.id.btn_point:if(clear_flag){clear_flag = false;editText.setText("");}editText.setText(input + ((Button)view).getText());break;case R.id.btn_plus:case R.id.btn_minus:case R.id.btn_multply:case R.id.btn_divide:if(clear_flag){clear_flag = false;input = "";editText.setText("");}editText.setText(input + " " + ((Button)view).getText() + " ");break;case R.id.btn_clear:clear_flag = false;input = "";editText.setText("");break;case R.id.btn_del:if(clear_flag){clear_flag = false;input = "";editText.setText("");}else if(input != null || !input.equals("")) {editText.setText(input.substring(0, input.length() - 1));}break;case R.id.btn_equal:getResult();break;}}//运算结果private void getResult(){String exp = editText.getText().toString();if(exp==null||exp.equals(""))return;if(!exp.contains(" "))return;if(clear_flag){clear_flag = false;return;}clear_flag = true;double result = 0;//运算符前的数字String s1 = exp.substring(0,exp.indexOf(" "));//运算符String op = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);//运算符后的数字String s2 = exp.substring(exp.indexOf(" ")+3);if(!s1.equals("")&&!s2.equals("")) {double d1 = Double.parseDouble(s1);double d2 = Double.parseDouble(s2);if (op.equals("+")) {result = d1 + d2;} else if (op.equals("-")) {result = d1 - d2;} else if (op.equals("*")) {result = d1 * d2;} else if (op.equals("/")) {if (d2 == 0)result = 0;elseresult = d1 / d2;}if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {int r = (int) result;editText.setText(r + "");} elseeditText.setText(result + "");}else if(!s1.equals("") && s2.equals("")){editText.setText(exp);}else if(s1.equals("") && !s2.equals("")){double d2 = Double.parseDouble(s2);if (op.equals("+")) {result = 0 + d2;} else if (op.equals("-")) {result = 0 - d2;} else if (op.equals("*")) {result = 0;} else if (op.equals("/")) {result = 0;}if (!s1.contains(".") && !s2.contains(".")) {int r = (int) result;editText.setText(r + "");} elseeditText.setText(result + "");}else {editText.setText("");}}
}

总结:一个简单的计算器就这样完成了,现在我们就可以使用自己写的计算器进行一些简单的计算了。

Android实现简单的计算器功能相关推荐

  1. android实现计算器功能吗,利用Android实现一个简单的计算器功能

    利用Android实现一个简单的计算器功能 发布时间:2020-11-20 16:25:01 来源:亿速云 阅读:90 作者:Leah 今天就跟大家聊聊有关利用Android实现一个简单的计算器功能, ...

  2. android计算器功能实现,在android中利用 studio实现一个简单的计算器功能

    在android中利用 studio实现一个简单的计算器功能 发布时间:2020-11-07 15:35:20 来源:亿速云 阅读:168 作者:Leah 这篇文章将为大家详细讲解有关在android ...

  3. php四则运算出题器_PHP实现的简单四则运算计算器功能示例

    本文实例讲述了PHP实现的简单四则运算计算器功能.分享给大家供大家参考,具体如下: php实现一个简单的四则运算计算器(还不支持括号的优先级).利用栈这种数据结构来计算表达式很赞. 这里可以使用栈的结 ...

  4. 编写简单的计算器功能的程序

    使用switch语句实现简单的加减乘除 具体代码如下: #include<stdio.h> int main() {int date1, date2; char op;printf(&qu ...

  5. C语言 编程实现简单的计算器功能

    编程实现简单的计算器功能,要求用户按如下格式从键盘输入算式: 操作数1 运算符op 操作数2 计算并输出表达式的值,其中算术运算符包括:加(+).减(-).乘().除(/).^(次幂). 要求使其能进 ...

  6. 编程实现简单的计算器功能(swich)

    编程实现简单的计算器功能,要求用户按如下格式从键盘输入算式:   操作数1 运算符op 操作数2   计算并输出表达式的值,其中运算符为:加( + ).减( - ),乘( * ).除( / )   用 ...

  7. android实现简单的记账功能

    android实现简单的记账功能 ps:这次作业真的有点折磨我,花了我断断续续三天的时间,其中有很多功能都是想实现,但是感觉就差一点点,由于截止时间快要到了,只能将还未实现的功能先搁置,后续会补充更多 ...

  8. php计算器按钮功能,PHP简单在线计算器功能示例

    PHP简单在线计算器功能示例 PHP可以被嵌入于HTML语言,它相对于其他语言.编辑简单,实用性强,更适合初学者.下面是小编分享的PHP简单在线计算器功能示例,一起来看一下吧. 简单的计算器(www. ...

  9. 给定两个整数,例如4和5,实现简单的计算器功能,即求出他们的和,差、积、商、余数。

    给定两个整数,例如4和5,实现简单的计算器功能,即求出他们的和,差.积.商.余数. #include<stdio.h> int main() {int IOperator,rOperato ...

最新文章

  1. linux写永久路由命令,用route命令添加永久路由(示例代码)
  2. Linux策略性路由应用及深入分析(iproute2)
  3. Rails I18n验证弃用警告
  4. 测测你写了多少行代码【转】
  5. 设计模式(9)----- 创建型模式-----工厂设计模式(抽象工厂模式)
  6. 超硬核详解SpringClould之Gateway网管【含源码例子】
  7. 2021年美赛成绩公布与美赛查询!美赛官网已更新
  8. Opencv入门——读写图像、读写像素、修改像素值
  9. 【洛谷题解】P1427 小鱼的数字游戏
  10. C盘清理(主要的大文件清理)
  11. flash驱动(一):Linux MTD子系统
  12. qtable sorting enable中文是按照什么顺序_漫威电影:22部电影观影顺序(附ZY)
  13. 前端打包工具webpack和Vite
  14. ubuntu使用xopen软件
  15. 【Caffe学习三】基于ROC-RK3399-PC/Ubuntu18.04的Caffe-SSD-CPU 安装编译___BUG
  16. IOS访问限制密码忘记解决方案
  17. Skype for Business Server 2015-04-前端服务器-2-创建一个文件共享
  18. 【Linux系列】Linux之CentOS操作系统日常问题(二):catalina.out占用内存大的问题
  19. D3D渲染技术之纹理
  20. 读书笔记之万历十五年

热门文章

  1. nginx 代理解决跨域问题
  2. 引力魔方扣费方式是什么?引力魔方有什么功能?
  3. Plus and Multiply
  4. k8s入坑之报错(11)添加node节点报错:error execution phase preflight: [preflight] Some fatal errors occurred:...
  5. Bzoj3441 乌鸦喝水
  6. sql注入之——SQLMap常见语句
  7. 【分享】集简云小程序识别名片到CRM流程搭建示例
  8. 亚马逊Amazon多账号操作攻略
  9. platform device和platform driver
  10. 一个老鼠走迷宫问题的python解法