Android studio制作计算器源代码

article/2025/9/19 8:41:11

版权声明:本文为博主原创文章,未经博主允许不得转载。https://mp.csdn.net/postedit/82623704

 

一、Android studio制作计算器源代码

这是我学Android 以来第一次制作计算器,Android学起来说难也不难,说简单也不简单

制作简易的计算器需要掌握基本布局,监听器,以及使用哪些主题等

废话不多说,直接上代码!

以下是activity_main.xml代码

<?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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><EditTextandroid:id="@+id/et_input"android:layout_width="match_parent"android:layout_height="60dp"android:paddingBottom="5dp"android:paddingRight="5dp"android:textSize="50sp" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_clr"android:layout_width="80dp"android:layout_height="80dp"android:text="C"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_del"android:layout_width="80dp"android:layout_height="80dp"android:text="D"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp" /><Buttonandroid:id="@+id/btn_div"android:layout_width="80dp"android:layout_height="80dp"android:text="÷"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp" /><Buttonandroid:id="@+id/btn_mul"android:layout_width="80dp"android:layout_height="80dp"android:text="×"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_7"android:layout_width="80dp"android:layout_height="80dp"android:text="7"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_8"android:layout_width="80dp"android:layout_height="80dp"android:text="8"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_9"android:layout_width="80dp"android:layout_height="80dp"android:text="9"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_sub"android:layout_width="80dp"android:layout_height="80dp"android:text="-"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_4"android:layout_width="80dp"android:layout_height="80dp"android:text="4"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_5"android:layout_width="80dp"android:layout_height="80dp"android:text="5"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_6"android:layout_width="80dp"android:layout_height="80dp"android:text="6"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_add"android:layout_width="80dp"android:layout_height="80dp"android:text="+"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"android:gravity="center_horizontal"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="80dp"android:layout_height="80dp"android:id="@+id/btn_1"android:text="1"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:layout_width="80dp"android:layout_height="80dp"android:id="@+id/btn_2"android:text="2"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:layout_width="80dp"android:layout_height="80dp"android:id="@+id/btn_3"android:text="3"android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"><Buttonandroid:layout_width="170dp"android:layout_height="80dp"android:id="@+id/btn_0"android:text="0"android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:layout_width="80dp"android:layout_height="80dp"android:id="@+id/btn_pt"android:text="."android:textSize="30sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout></LinearLayout><Buttonandroid:id="@+id/btn_eq"android:layout_width="80dp"android:layout_height="170dp"android:layout_marginLeft="10dp"android:text="="android:textSize="30sp"android:paddingRight="15sp"android:paddingBottom="15sp" /></LinearLayout></LinearLayout>

完成以上代码,你就会看到这样一个效果图

效果如下

如果你觉得这个计算器有点不好看,可以在按钮里面给元素添加颜色

完成简易计算器布局之后就给按钮添加点击事件以及按钮效果

 

以下是MainActivity.java代码

package com.example.a0909_homework_04;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends AppCompatActivity implements View.OnClickListener {//创建Button对象   也就是activity_main.xml里所设置的IDButton btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;Button btn_mul,btn_div,btn_add,btn_sub;Button btn_clr,btn_del,btn_eq;EditText et_input;boolean clr_flag;    //判断et编辑文本框中是否清空@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//实例化对象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_pt= (Button) findViewById(R.id.btn_pt);btn_add= (Button) findViewById(R.id.btn_add);btn_sub= (Button) findViewById(R.id.btn_sub);btn_mul= (Button) findViewById(R.id.btn_mul);btn_div= (Button) findViewById(R.id.btn_div);btn_clr= (Button) findViewById(R.id.btn_clr);btn_del= (Button) findViewById(R.id.btn_del);btn_eq= (Button) findViewById(R.id.btn_eq);et_input= (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_pt.setOnClickListener(this);btn_add.setOnClickListener(this);btn_sub.setOnClickListener(this);btn_mul.setOnClickListener(this);btn_div.setOnClickListener(this);btn_clr.setOnClickListener(this);btn_del.setOnClickListener(this);btn_eq.setOnClickListener(this);}@Overridepublic void onClick(View v) {String str=et_input.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_pt:if(clr_flag){clr_flag=false;str="";et_input.setText("");}et_input.setText(str+((Button)v).getText());break;case R.id.btn_add:case R.id.btn_sub:case R.id.btn_mul:case R.id.btn_div:if(clr_flag){clr_flag=false;str="";et_input.setText("");}if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {str=str.substring(0,str.indexOf(" "));}et_input.setText(str+" "+((Button)v).getText()+" ");break;case R.id.btn_clr:if(clr_flag)clr_flag=false;str="";et_input.setText("");break;case R.id.btn_del: //判断是否为空,然后在进行删除if(clr_flag){clr_flag=false;str="";et_input.setText("");}else if(str!=null&&!str.equals("")){et_input.setText(str.substring(0,str.length()-1));}break;case R.id.btn_eq: //单独运算最后结果getResult();//调用下面的方法break;}}private void getResult() {String exp=et_input.getText().toString();if(exp==null||exp.equals("")) return ;//因为没有运算符所以不用运算if(!exp.contains(" ")){return ;}if(clr_flag){clr_flag=false;return;}clr_flag=true;//截取运算符前面的字符串String s1=exp.substring(0,exp.indexOf(" "));//截取的运算符String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);//截取运算符后面的字符串String s2=exp.substring(exp.indexOf(" ")+3);double cnt=0;if(!s1.equals("")&&!s2.equals("")){double d1=Double.parseDouble(s1);double d2=Double.parseDouble(s2);if(op.equals("+")){cnt=d1+d2;}if(op.equals("-")){cnt=d1-d2;}if(op.equals("×")){cnt=d1*d2;}if(op.equals("÷")){if(d2==0) cnt=0;else cnt=d1/d2;}if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {int res = (int) cnt;et_input.setText(res+"");}else {et_input.setText(cnt+"");}}//如果s1是空    s2不是空  就执行下一步else if(!s1.equals("")&&s2.equals("")){double d1=Double.parseDouble(s1);if(op.equals("+")){cnt=d1;}if(op.equals("-")){cnt=d1;}if(op.equals("×")){cnt=0;}if(op.equals("÷")){cnt=0;}if(!s1.contains(".")) {int res = (int) cnt;et_input.setText(res+"");}else {et_input.setText(cnt+"");}}//如果s1是空    s2不是空  就执行下一步else if(s1.equals("")&&!s2.equals("")){double d2=Double.parseDouble(s2);if(op.equals("+")){cnt=d2;}if(op.equals("-")){cnt=0-d2;}if(op.equals("×")){cnt=0;}if(op.equals("÷")){cnt=0;}if(!s2.contains(".")) {int res = (int) cnt;et_input.setText(res+"");}else {et_input.setText(cnt+"");}}else {et_input.setText("");}}
}

以上代码写好,简易的计算器就完成了!!!

 

 


http://chatgpt.dhexx.cn/article/Brv8vUgP.shtml

相关文章

Android入门之简易计算器(一)

Android Android入门之简易计算器&#xff08;一&#xff09; 文章目录 AndroidAndroid入门之简易计算器&#xff08;一&#xff09;[TOC](文章目录) 前言一、项目结构二、界面显示三、前端界面1.界面分布2.文本定义在了strings.xml里面3、圆形按钮创建 四、前端页面总代码五、…

Android 实战项目:简单计算器

文章目录 实战项目&#xff1a;简易计算器1.需求分析2.界面设计3.关键代码1&#xff0e;输入按键的合法性校验2&#xff0e;执行运算并显示计算结果 简单计算器 - 详细操作步骤总结 实战项目&#xff1a;简易计算器 1.需求分析 虽然只学了一些Android的简单控件&#xff0c;但…

Android Studio制作简单计算器

代码地址 https://github.com/xjhqre/android_calculator 效果演示 1. 连续加法 2. 连续减法 3. 连续乘法 4. 连续除法 5. 优先级运算 6. 退格功能 7. 错误提示 制作步骤 1. 创建按钮图片 1.1. 退格图形 <vector xmlns:android"http://schemas.android.com/apk/…

【Android应用开发之前端——简单计算器效果】

1.完成计算器布局 整个计算器界面主要分为两部分&#xff0c;一部分是上面的文本框&#xff0c;用于显示计算结果&#xff1b;另一部分是下面的几排按钮&#xff0c;用户输入数字与各种运算符。为了减少复杂度&#xff0c;我们可以精简一些功能&#xff0c;只保留数字与加、减…

使用Android 实现计算器功能

使用android实现简易的计算器的功能 1、给计算机布局&#xff1a;activity_main_xml&#xff1a; <?xml version"1.0" encoding"utf-8"?> <GridLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"h…

android studio实现计算器界面

实现计算器界面 新建项目1、打开Android Studio2、创建项目3、设置项目基本信息4、等待项目文件加载 打开界面文件1、设置线性布局2、增加子容器3、添加内容 计算器界面完成效果 新建项目 1、打开Android Studio 2、创建项目 点击右上角【New Project】选择“Empty Activity”…

Android Studio实现计算器功能

实验一&#xff1a;做一个简单的计算器 1.创建布局文件Activity_main.xml 代码如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:orient…

Android Studio入门教程(计算器)

一、建立开发环境 1、AS简介 Android Studio 是Google开发的一款面向Android开发者的IDE&#xff0c;支持Windows、Mac、Linux等操作系统&#xff0c;基于流行的开发语言java集成开发环境IntelliJ搭建而成的&#xff0c;类似Eclipse ADT。该IDE在2003年5月的Google I/O开发者…

Android开发——简单计算器实现

计算器项目&#xff0c;要求实现加、减、乘、除、求倒数、求平方根等简单运算。 真机调试结果如下图&#xff1a; 布局文件&#xff1a;main_activity.xml <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://sc…

Android Studio简易计算器

目录 第一步&#xff0c;创建新项目 第二步&#xff0c;设计UI 第三步&#xff0c;实现计算逻辑 第四步&#xff0c;测试应用程序 随着移动互联网的普及&#xff0c;手机应用程序已经成为人们生活中不可或缺的一部分。计算器是一类被广泛使用的应用程序之一&#xff0c;因此…

十五、吉布斯采样的理解

由于本人喜欢在纸上手推原理&#xff0c;所以附上照片&#xff0c;欢迎提出建议

MCMC、吉布斯采样

学习视频&#xff1a;B站白板推导 学习和代码教程&#xff1a; Li Hang code 知乎&#xff08;和上面内容一模一样&#xff09; &#xff08;权当参考&#xff09; 关于上面代码中&#xff0c;吉布斯采样二维正态分布的理解&#xff1a; Σ的意思是协方差矩阵&#xff0c;在…

LDA----吉布斯采样

w~Mult(w|p) 这里可以引入一个新的概念:概率图模型,来画出这种模型。如图 3-1所示,图中被涂色的 表示可观测变量,方框表示重复抽取的次数, 表示一篇文档中总共 个单词, 表示M篇文档。也就是说,重复抽取 篇文档,每个文档抽取 个单词,这样的生成模型生成了整个语料(corpus)。 总结…

LDA-模型的实现-----吉布斯采样

https://www.cnblogs.com/nlp-yekai/p/3858705.html?utm_sourcetuicool&utm_mediumreferral 算法 LDA Collapsed Gibbs Sampling 输入&#xff1a;文档集(分词后)&#xff0c;K(主题数)&#xff0c;α&#xff0c;β&#xff0c;iter_number(迭代次数) 输出&#xff1a;…

R语言实现MCMC中的Metropolis–Hastings算法与吉布斯采样

创建测试数据 第一步&#xff0c;我们创建一些测试数据&#xff0c;用来拟合我们的模型。我们假设预测变量和因变量之间存在线性关系&#xff0c;所以我们用线性模型并添加一些噪音。 trueA <- 5trueB <- 0trueSd <- 10sampleSize <- 31# 创建独立的x值x <- (…

马尔科夫过程与吉布斯采样

随机模拟(或者统计模拟)方法有一个很酷的别名是蒙特卡罗方法(Monte Carlo Simulation)。这个方法的发展始于20世纪40年代&#xff0c;和原子弹制造的曼哈顿计划密切相关&#xff0c;当时的几个大牛&#xff0c;包括乌拉姆、冯.诺依曼、费米、费曼、Nicholas Metropolis&#xf…

吉布斯采样的简单描述

几个可以学习gibbs sampling的方法1&#xff0c;读Bishop的Pattern Recognition and Machine Learning&#xff0c;讲的很清楚&#xff0c;但是我记得好像没有例子。2&#xff0c;读artificial Intelligence&#xff0c;2、3版&#xff0c;都有。但是我没读过。3&#xff0c;最…

【ML】线性回归的吉布斯采样(Gibbs Sampling)实现(python)

导航 Bayesian Linear RegressionGibbs SamplingDerving a Gibbs samplerUpdate for β 0 \beta_0 β0​Update for β 1 \beta_1 β1​Update for τ \tau τSynthetic dataGibbs sampler code downlaodReferences Bayesian Linear Regression 考虑只有一个自变量(indepen…

【机器学习】主题建模+隐狄利克雷分配模型(LDA)+吉布斯采样

【主题建模】 大数据时代&#xff0c;面对海量的数据&#xff0c;如果能知道它的主题是什么&#xff0c;从数据压缩的角度来看&#xff0c;我们可以通过很少量的主题来管理很大亮的文档数据集合&#xff0c;从而实现一个比较简洁的操作和管理文档集合的目的&#xff1b;除此之外…

【人工智能】对贝叶斯网络进行吉布斯采样

问题 现要求通过吉布斯采样方法&#xff0c;利用该网络进行概率推理&#xff08;计算 P(RT|SF, WT)、P2(CF|WT)的概率值&#xff09;。 原理 吉布斯采样的核心思想为一维一维地进行采样&#xff0c;采某一个维度的时候固定其他的维度&#xff0c;在本次实验中&#xff0c;假…