1、实验目的和要求

(1)掌握Android中常见的几种布局;
(2)掌握Android中常见的控件
(3)掌握事件处理方法

2.activity_main界面的代码

<?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="106dp"android:paddingRight="5dp"android:paddingBottom="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:layout_marginTop="10dp"android:gravity="center_horizontal"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"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_1"android:layout_width="80dp"android:layout_height="80dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="1"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_2"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="2"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_3"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="3"android:textSize="30sp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_0"android:layout_width="170dp"android:layout_height="80dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="0"android:textSize="30sp" /><Buttonandroid:id="@+id/btn_pt"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="."android:textSize="30sp" /></LinearLayout></LinearLayout><Buttonandroid:id="@+id/btn_eq"android:layout_width="80dp"android:layout_height="170dp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"android:text="="android:textSize="30sp" /></LinearLayout></LinearLayout>

3.MainActivity的代码

package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;import androidx.appcompat.app.AppCompatActivity;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);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_pt= findViewById(R.id.btn_pt);btn_add= findViewById(R.id.btn_add);btn_sub= findViewById(R.id.btn_sub);btn_mul= findViewById(R.id.btn_mul);btn_div= findViewById(R.id.btn_div);btn_clr= findViewById(R.id.btn_clr);btn_del= findViewById(R.id.btn_del);btn_eq= findViewById(R.id.btn_eq);et_input= 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("");}}
}

4.结果实验截图


Android制作简单的计算器相关推荐

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

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

  2. 【MATLAB-app】系列教程(含视频)第2课_实例:使用appdesigner 制作简单的计算器

    写作时间: 2020-12-24 我的教程目录 第1课:初步了解appdesigner 第2课:学会制作一个计算器 第3课:学习更多的控件 第4课:一个简单图像处理软件制作 第5课:学习APPdesi ...

  3. 用Java制作简单的计算器

    目录 界面的设计: 事件的响应: 计算: 详细代码如下: 总结: 要制作一个简单的计算器,首先就是对于界面的设计,然后就是其功能的实现. 对于事件的实现大概就分下面几个步骤. 确定事件源和监听源 实现 ...

  4. Android实现简单的计算器

    android的布局和配置文件 1.下面就是android实现的计算器的布局配置,可以看见基本是线性布局,这样的好处是界面简洁明了 <LinearLayout xmlns:android=&qu ...

  5. JAVA制作简单的计算器

    今天我们来学习做一个桌面的简单计算器.之所以说简单,是因为能完成的运算简单,只有加减乘除四则运算,远远不能跟那些功能完备的科学计算器相比.而且其中实现的思路也很简单. 关键词:java计算器,简单计算 ...

  6. c语言制作简单的计算器

    #include<stdio.h> #include<stdlib.h> #include<memory.h> char ch={0};//单个字符 int num ...

  7. js制作简单的计算器

    eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法: eval(string): <!DOCTYPE html> <html lang=" ...

  8. C++之MFC制作简单计算器(VS2019实现),附带完整代码

    资源下载 代码下载 代码失效可以联系本人: 15377920718 效果演示 MFC制作简单的计算器(VS2019实现) VS2019可以用于制作简单的计算器,用的是MFC,通过设立button以及编 ...

  9. android页面布局计算机,Android Studio制作简单计算器App

    Android Studio制作简单计算器App 计算机界面如图: 程序设计步骤: (1)在布局文件中声明编辑文件框EditText,按钮Button等组件. (2)在MainActivity中获取组 ...

最新文章

  1. 十种工具审核网络安全
  2. 零基础python入门编程全栈量化ai 百度云_Python编程量化AI全栈零基础入门帖子详情 - 网易云课堂...
  3. TI xDAIS的深入理解
  4. python爬虫怎么爬小说_python从爬虫基础到爬取网络小说实例
  5. 理解CSS3中的background-size(对响应性图片等比例缩放)
  6. [BUUCTF-pwn]——jarvisoj_fm
  7. 指向类的数据成员和函数指针
  8. 用python连接数据库_用Python连接MySQL
  9. 逸出 java_【java】知识系谱-基础篇-线程-发布、逸出
  10. ts定义数组类型_TS的对象类型、数组类型、函数类型
  11. ssh远程登陆 Ubuntu虚拟机出错,配置ssh服务-转
  12. 《转》Babel 入门教程
  13. python凯撒密码流程图_python实现凯撒密码、凯撒加解密算法
  14. VSD Viewer for Mac(Visio绘图文件阅读器)
  15. Reactor(反应器)模式
  16. html js鼠标滚轮事件,JS事件-鼠标滚轮事件
  17. ​2020 年大前端技术趋势解读
  18. UGUI的DrawCall和Rebuild
  19. 强烈推荐33个 GitHub 前端学习资源
  20. Axure第一周学习日志

热门文章

  1. android imageview stretchblt,想知道Bmp图像的缩小放大用Tcanvas.StretchDraw还是用StretchBlt函数?(50分)...
  2. execution常用表达式整理
  3. sqlite3数据库损坏修复
  4. [转载]海康摄像头_2
  5. 安全认证框架Shiro (二)- shiro过滤器工作原理
  6. Andriod PDA RFID感应盘点
  7. 机器学习常见任务类型
  8. 字典的基础及字典的使用实例
  9. 通达信 数据格式 java_通达信日线 数据格式
  10. Think Python读书笔记及课后习题---【前三章】