具体源代码

MainActivity.java

package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{Button btn_clean,btn_del,btn_divide,btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_multiply,btn_add,btn_minus,btn_point,btn_equal;TextView textView;boolean clear_flag;                    //清空标识protected void onCreate(Bundle savedInstanceState) {super.onCreate (savedInstanceState);setContentView (R.layout.activity_main);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;str = "";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;default:break;}}//算法private void getResult() {String xiao = textView.getText().toString();if (xiao == null || xiao.equals("")) {return;}if (!xiao.contains("")) {return;}if (clear_flag) {clear_flag = false;return;}clear_flag = true;String str1 = xiao.substring(0, xiao.indexOf(" "));                      // 获取到运算符前面的字符String str_y = xiao.substring(xiao.indexOf(" ") + 1, xiao.indexOf(" ") + 2);    //获取到运算符String str2 = xiao.substring(xiao.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(".") && !xiao.equals("÷")) {int k = (int) result;               //强制转换textView.setText(k + "");} else {textView.setText(result + "");}} else if (!str1.equals("") && str2.equals("")) {textView.setText(xiao);} else if (str1.equals("") && !str2.equals("")) {double num2 = Double.parseDouble(str2);if (xiao.equals("+")) {result = 0 + num2;} else if (xiao.equals("-")) {result = 0 - num2;} else if (xiao.equals("×")) {result = 0;} else if (xiao.equals("/")) {result = 0;}if (!str2.contains(".")) {int r = (int) result;textView.setText(r + "");} else {textView.setText(result + "");}} else {textView.setText("");}}
}

activity_main.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="60dp"android:singleLine="true"android:layout_margin="15dp"android:layout_width="match_parent"android:layout_height="150dp"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>

drawable中建change.xml  gray.xml  orange.xml  white.xml  四个

change.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/gray"/>           //默认颜色<item android:drawable="@drawable/orange" android:state_pressed="true"/></selector>

gray.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp"/><solid android:color="#f9f9f9"/><strokeandroid:width="2dp"android:color="#ffa600"/></shape>

orange.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp"/>               // 圆角<solid android:color="#F7B684"/>         //颜色
</shape>

white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="5dp"/><solid android:color="#ffffff"/><strokeandroid:width="1dp"android:color="#ffa600"/></shape>

若是在poject下运行不了,可以试一下在android下运行,算法写得不太好,请见谅!

用安卓写一个简单的计算器 android studio相关推荐

  1. python123程序设计题说句心里话_用c++写一个简单的计算器程序

    // 050305.cpp : 定义控制台应用程序的入口点. // // 050304.cpp : 定义控制台应用程序的入口点. // //四则运算 #include "stdafx.h&q ...

  2. java 写一个简单的计算器

    请点击好的,看完介绍,咱们就根据这个杭电oj上1237 这道题,改写一个用GUI写出来的 小计算器,类似于这样: 然后 通过输入 显示结果,比如说: 可以看得出来,咱们得到的结果是正确的: 代码: p ...

  3. PythonGUI 使用Tkinter写一个简单时间间隔计算器

    python作业里要求写时间间隔计算器,突然想到可以自学GUI直接做成简单的计算器,于是有了这个简单程序,如有错误,欢迎指正. 代码如下: 此部分是输入开始时间和结束时间的代码 import tkin ...

  4. 你已经学会了基本的c语言了,来尝试写一个简单的计算器吧

    关于原问题 第一行输入两个整数,用空格分开: 第二行输入一个运算符(+.-.*./). 所有运算均为整数运算,保证除数不包含0. 这里我们选择switch函数 switch(c){case '+':p ...

  5. 用python语言写一个简单的计算器

    假如我们有这样一个式子: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2 ...

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

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

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

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

  8. android三角形切图软件,还在为小三角形切图?使用纯CSS写一个简单的三角形

    同学们,当美工给的设计图是这样: 或者这样: 我的内心其实是拒绝的-_-:但工作还得干,大部分同学会写 .icon{width:20px;height:20px;display:block;margi ...

  9. 计算器android studio代码,Android studio实现简单计算器

    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析 在Android studio中设计并实现一个简单的计算器,实现连续的加减乘除运算. 界面 ...

最新文章

  1. 连接服务器_命令行连接FTP服务器
  2. 独家 | 5大冠军出炉!李开复的AI挑战赛,冠军们聊到了这么些感受
  3. 木棍分割[HAOI2008]
  4. Java中多线程启动,为什么调用的是start方法,而不是run方法?
  5. 笔记本电脑linux系统下载,给笔记本电脑装Linux系统
  6. html选择器_css的9个常用选择器
  7. linux mysql csv文件_Linux MySQL数据库如何导出数据文件?导出csv语句命令
  8. Ubuntu16.04安装Mono4.2.1 C#开发环境
  9. Ubuntu 16.04安装SoapUI工具进行接口测试(Web Service/WSDL/RESTfull)
  10. 计算机普通话培训开班简报,普通话培训第四期简报.doc
  11. jmeter TCP接口压力测试
  12. 射频识别系统的组成及工作原理解析
  13. Shell中IF大于等于小于
  14. Muse-UI自定义主题的使用方法
  15. 【Angular】@Input和@Output
  16. PHP时间差七个小时怎么回事,php 怎么解决8小时时间差的问题
  17. 你绝对不知道的head标签
  18. 使用arpspoof进行arp欺骗
  19. java 获取当年_java获取当年第一天
  20. 空气流量传感器原理解析

热门文章

  1. 兼职APP开发的市场分析情况
  2. CERO二次开发依赖模型参数清单(失败)
  3. CPU卡程序设计实例(六)ETU配置
  4. 说说Intel的驱动
  5. 2021FME博客大赛 —— 杭州城市西湖区活力评价
  6. 『Halcon与C#混合编程』第二章01_迈德威视工业相机SDK入门
  7. 安卓手机ROM之 DIY纪实
  8. 指令系统-CISC和RISC的区别
  9. java 实现的excel数据导入及导入模板下载
  10. 最长单调递增子序列 python_最长单调递增子序列