.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#D8ECF3"><TextViewandroid:gravity="bottom|right"android:textSize="70dp"android:singleLine="true"android:layout_margin="15dp"android:layout_width="match_parent"android:layout_height="120dp"android:background="@drawable/white"android:id="@+id/textView"/><TableRowandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/btn_clean"android:layout_marginLeft="10dp"android:background="@drawable/orange"android:gravity="center"android:text="C"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_del"android:layout_marginLeft="10dp"android:layout_span="2"android:background="@drawable/gray"android:gravity="center"android:text="Del"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_divide"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@drawable/gray"android:gravity="center"android:layout_span="1"android:text="/"android:textSize="25sp" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/btn_7"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="7"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_8"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="8"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_9"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="9"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_multiply"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@drawable/gray"android:gravity="center"android:text="*"android:textSize="25sp" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/btn_4"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="4"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_5"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="5"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_6"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="6"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_add"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@drawable/gray"android:gravity="center"android:text="+"android:textSize="25sp" /></TableRow><TableRowandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/btn_1"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="1"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_2"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="2"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_3"android:layout_marginLeft="10dp"android:background="@drawable/white"android:gravity="center"android:text="3"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_minus"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@drawable/gray"android:gravity="center"android:text="-"android:textSize="25sp" /></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/btn_0"android:layout_marginLeft="10dp"android:layout_span="2"android:background="@drawable/white"android:gravity="center"android:text="0"android:textSize="25sp" /><Buttonandroid:id="@+id/btn_point"android:layout_marginLeft="10dp"android:layout_span="1"android:background="@drawable/white"android:gravity="center"android:text="."android:textSize="25sp" /><Buttonandroid:id="@+id/btn_equal"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_span="1"android:background="@drawable/gray"android:gravity="center"android:text="="android:textSize="25sp" /></TableRow></TableLayout>

.java文件:

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class dj_Activity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_dj_);btn_0 = findViewById(R.id.btn_0);        //初始化btn_1 = findViewById(R.id.btn_1);btn_2 = findViewById(R.id.btn_2);btn_3 = findViewById(R.id.btn_3);btn_4 = findViewById(R.id.btn_4);btn_5 = findViewById(R.id.btn_5);btn_6 = findViewById(R.id.btn_6);btn_7 = findViewById(R.id.btn_7);btn_8 = findViewById(R.id.btn_8);btn_9 = findViewById(R.id.btn_9);btn_multiply = findViewById(R.id.btn_multiply);btn_divide = findViewById(R.id.btn_divide);btn_add = findViewById(R.id.btn_add);btn_minus = findViewById(R.id.btn_minus);btn_point = findViewById(R.id.btn_point);btn_del =findViewById(R.id.btn_del);btn_equal = findViewById(R.id.btn_equal);btn_clean = findViewById(R.id.btn_clean);textView = findViewById(R.id.textView);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_minus.setOnClickListener(this);btn_multiply.setOnClickListener(this);btn_del.setOnClickListener(this);btn_divide.setOnClickListener(this);btn_point.setOnClickListener(this);btn_add.setOnClickListener(this);btn_equal.setOnClickListener(this);btn_clean.setOnClickListener(this);}public void onClick(View v) {String str = textView.getText().toString();switch(v.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;str="";textView.setText ("");}textView.setText(str+((Button)v).getText ());break;case R.id.btn_add:case R.id.btn_minus:case R.id.btn_multiply:case R.id.btn_divide:if(clear_flag){clear_flag=false;textView.setText("");}textView.setText(str+" "+((Button)v).getText()+" ");break;case R.id.btn_del:if(clear_flag){clear_flag=false;textView.setText ("");}else if (str != null && !str.equals ("")){textView.setText(str.substring(0,str.length()-1));    //删除一个字符}break;case R.id.btn_clean:clear_flag=false;str = "";textView.setText("");        //清空文本内容break;case R.id.btn_equal:getResult();              //获取结果break;}}private void getResult() {                            //算法String s = textView.getText().toString();if(s == null  || s.equals ("")){return;}if (!s.contains ("")){return;}if (clear_flag){clear_flag=false;return;}clear_flag=true;String str1 = s.substring(0,s.indexOf(" "));                      // 获取到运算符前面的字符String str_y = s.substring(s.indexOf(" ")+1,s.indexOf(" ")+2);    //获取到运算符String str2 = s.substring(s.indexOf(" ")+ 3);                     //获取到运算符后面的字符double result = 0;if (!str1.equals ("")  && !str2.equals ("")){double num1 = Double.parseDouble(str1);   //将str1、str2强制转化为double类型double num2 = Double.parseDouble(str2);if (str_y.equals ("+")){result = num1 + num2;}else if (str_y.equals ("-")){result = num1 - num2;}else if (str_y.equals ("÷")){if (num2 == 0){result = 0;}else {result = num1/num2;}}else if (str_y.equals ("*")){result = num1*num2;}if (!str1.contains (".") && !str2.contains (".") && !s.equals ("÷")){int k = (int) result;               //强制转换textView.setText (k);}else{textView.setText (result+"");}}else if (!str1.equals ("") && str2.equals ("")){textView.setText (s);}else if (str1.equals ("") && !str2.equals ("")){double num2 = Double.parseDouble(str2);if (s.equals ("+")){result = 0 + num2;}else if (s.equals("-")){result = 0 - num2;}else if (s.equals("×")){result = 0;}else if (s.equals("÷")){result = 0;}if (!str2.contains (".")) {int r = (int) result;textView.setText (r + "");} else {textView.setText (result + "");}} else {textView.setText ("");}}
}

截图:

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

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

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

  2. android实现计算器功能吗,简单实现Android计算器功能

    自己写的安卓的计算器: 注:这个是在mac中开发的,如果要在windows的eclipse中运行可能会出现路径问题,解决办法从windows中已有的安卓工程根目录下复制一下classpath文件,然后 ...

  3. android实现计算器功能吗,安卓实现一个计算器的功能

    新建项目工程 第一步 布局的设置 android:layout_width="368dp" android:layout_height="495dp" andr ...

  4. android计算器开发论文,基于Android计算器功能的实现毕业设计论文

    <基于Android计算器功能的实现毕业设计论文.doc>由会员分享,可免费在线阅读全文,更多与<基于Android计算器功能的实现毕业设计论文>相关文档资源请在帮帮文库(ww ...

  5. Android实现简单的计算器功能

    Android实现简单的计算器功能 **前言:**通过Android实现简单的计算器功能,实现简单的加.减.乘.除操作. 效果图如下: 第一步 布局文件:activity_main.xml <? ...

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

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

  7. Java仿小米计算器源码_xiaomi-Calculator 仿照小米的计算器功能,作为一个Android的初学者可以参考,界面简单大方。 259万源代码下载- www.pudn.com...

    文件名称: xiaomi-Calculator下载  收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 795 KB 上传时间: 2016-06-13 下载次数: 0 提 ...

  8. android角度计算器,Calckit高级版一款安卓多功能计算器,内置高度定制的科学计算器...

    前言 手机中的计算器功能你多久用一次呢,我相信对于多数人来说计算器是一个低频使用软件. 其原因可能是用不上计算功能也很可能是单一的计算功能并不能满足你对于计算的需求. 那么如果现在有一款可以完成中学到 ...

  9. Android 房租计算器,简单计算器

    房租计算器 功能简介 1.根据入住时间,计算入住至今的月份间隔,x月x天: 2.自定义计算器,无括号,简单计算器: 3.使用SqLite存储计算结果,使用RecyclerView显示数据,实现刷新和加 ...

最新文章

  1. 据我所知,这是第一个完整实现运动分割、动态目标追踪等的「开源」动态SLAM系统!...
  2. Spark SQL 源代码分析系列
  3. Matlab | Matlab从入门到放弃(11)——基于Matlab的矩阵分解、幂和指数
  4. 关于前端设置cookie
  5. C#3.0 为我们带来什么(3) —— 初始化器
  6. 疲劳综合征的原因及解决办法
  7. .net 这些年发展 参考资料
  8. 【MyBatis】动态SQL中的参数判空
  9. pulse 去马赛克软件_一款号称能去马赛克的软件,却在面对游戏角色时“翻了车”...
  10. Cain嗅探工具的https数据捕获
  11. HTML5如何把圆分成六等分,CSS八等分圆的实现示例_CSS教程_CSS
  12. MySQL学习笔记(七)视图,触发器,事务,存储过程,函数,流程控制,索引
  13. docker修改nginx容器主页内容信息【还能这样操作?】
  14. linux双网卡网桥转发,Linux网桥实现
  15. C语言:输入一个整数 求它是几位数
  16. 啃完这些Java面试题,面试阿里P7稳了
  17. BZOJ P1856 字符串
  18. 木子-前端-方法标签属性小记(普通jsp/html篇)2018
  19. 高维数组matlab,MATLAB 高维数组
  20. 计算机配置主板技术参数,怎么看电脑的配置?教你4个方法查看新电脑pc配置硬件参数...

热门文章

  1. 【叮咚买菜】叮咚抢菜使用教程
  2. java linux 字体设置_linux下面java字体的设置
  3. 问题 A: Jugs
  4. div html 下边加横线_css字体下边横线 html超链接更改颜色和去掉下划线
  5. 怎么判断一篇微信公众号文章阅读量是不是刷上来的?
  6. ssl证书生成 详细流程
  7. 涉密计算机打印机共享案例分析,案例教程|兼收并蓄,有线打印机无线共享实战...
  8. 《重构-改善既有代码的设计》第三章(上)
  9. re 正则表达式匹配中文
  10. (域名解析过程)web请求过程全解析一