简单计算器的设计是学习安卓的第一个APP的设计,我发现,其实安卓跟Java有很大的相似性,虽然界面的设计要用到前端的设计,但是功能的实现基本都是使用Java来实现的,但由于对Android studio和虚拟器的不熟悉以及对Java的掌握度不高,所以在进行设计的时候遇到了很多问题。

(1)最先遇到的问题是蓝叠中国突然连接不上,而且连接上之后第二天可能就又出问题了,之前是可以的,后来就上网找原因,发现可能是adb那里的端口被占用了,可以先去验证一下,然后再重新连,可是后来在连接的时候又显示积极拒绝连接,可能是虚拟机那里出问题了。由于手机系统不同,所以就无法连接手机进行界面的实现,后来就想着能不能用回Android自身的虚拟器,发现说可能是emulator和haxm的文件不完全,要先将这两个文件下载好,然后再去BIOS那里修改设置,发现是可以的,只是自身带的虚拟机还是太大,运行太慢,所以还是去弄蓝叠中国了。

(2)进入了模拟器了,但是由于蓝叠中国的界面是长方形的,而在Android studio设置的preview是长方形的,所以有些地方显示不出来,所以在建立一个项目之前需要设置一下界面。

实现的界面如下:

设计步骤:

(1)在布局文件中声明按钮等;

(2)在activity中获得实例;

(3)调用getText方法进行加减乘除运算。

源代码如下:

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"><EditText                                        对页面进行布局android:id="@+id/tv_label"android:layout_width="match_parent"android:layout_height="60dp"android:paddingBottom="5dp"android:paddingRight="5dp"android:textSize="30sp"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_clear"android:layout_width="50dp"android:layout_height="50dp"android:text="C"android:textSize="20sp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_1"android:text="1"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:paddingRight="15sp"android:paddingBottom="15sp" /><Buttonandroid:id="@+id/btn_2"android:text="2"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp" /><Buttonandroid:id="@+id/btn_3"android:text="3"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp" /><Buttonandroid:id="@+id/btn_plus"android:text="+"android:textSize="20sp"android:layout_width="50dp"android:layout_height="50dp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_4"android:text="4"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_5"android:text="5"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_6"android:text="6"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_reduce"android:text="-"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_7"android:text="7"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_8"android:text="8"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_9"android:layout_width="50dp"android:layout_height="50dp"android:text="9"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_time"android:text="*"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:orientation="horizontal"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_point"android:text="."android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_0"android:text="0"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:paddingRight="20sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_equal"android:text="="android:layout_width="50dp"android:layout_height="50dp"android:layout_marginLeft="10dp"android:textSize="20sp"android:paddingRight="15sp"android:paddingBottom="15sp"/><Buttonandroid:id="@+id/btn_divide"android:text="/"android:layout_width="50dp"android:layout_height="50dp"android:textSize="20sp"android:layout_marginLeft="10dp"android:paddingRight="15sp"android:paddingBottom="15sp"/></LinearLayout>
</LinearLayout>

这一次选择了LinearLayout布局,因为对比起来,对该布局了解的更深,使用起来更加简单一些

MainActivity源代码如下:

package com.xdw.jisuanji;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 btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0;Button btn_plus,btn_clear,btn_reduce,btn_time,btn_point,btn_equal,btn_devide;EditText tv_label;boolean clear_flag;  //清空标识@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);    //将对象实例化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_0=(Button)findViewById(R.id.btn_0);btn_plus=(Button)findViewById(R.id.btn_plus);btn_clear=(Button)findViewById(R.id.btn_clear);btn_reduce=(Button)findViewById(R.id.btn_reduce);btn_time=(Button)findViewById(R.id.btn_time);btn_point=(Button)findViewById(R.id.btn_point);btn_equal=(Button)findViewById(R.id.btn_equal);btn_devide=(Button)findViewById(R.id.btn_divide);tv_label=(EditText)findViewById(R.id.tv_label);  //实例化之后的显示屏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_0.setOnClickListener(this);btn_plus.setOnClickListener(this);btn_clear.setOnClickListener(this);btn_reduce.setOnClickListener(this);btn_time.setOnClickListener(this);btn_point.setOnClickListener(this);btn_equal.setOnClickListener(this);btn_devide.setOnClickListener(this);}@Overridepublic void onClick(View v){String str=tv_label.getText().toString();switch(v.getId()){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_0:case R.id.btn_point:if(clear_flag){clear_flag=false;str="";tv_label.setText("");}tv_label.setText(str+((Button)v).getText());break;case R.id.btn_divide:case R.id.btn_plus:case R.id.btn_reduce:case R.id.btn_time:if(clear_flag){clear_flag=false;str="";tv_label.setText("");}tv_label.setText(str+""+((Button)v).getText()+"");break;case R.id.btn_clear:clear_flag=false;str="";tv_label.setText("");case R.id.btn_equal:          //单独运算最后结果getResult();          //调用函数break;}}private void getResult() {String string=tv_label.getText().toString();if(string==null || string.equals(""))  return;if (!string.contains("")){return;}if (clear_flag){clear_flag=false;return;}clear_flag=false;String s1=string.substring(0,string.indexOf(""));     //运算符前面的字串符String option=string.substring(string.indexOf("")+1,string.indexOf("")+2);//截取的运算符String s2=string.substring(string.indexOf("")+3);    //截取运算符后面的字符串double result=0;if (!s1.equals("")&&!s2.equals("")){           //s1,s2均不为空double d1=Double.parseDouble(s1);double d2=Double.parseDouble(s2);if (option.equals("+")) {result = d1 + d2;}else if (option.equals("-")){result=d1-d2;}else if (option.equals("*")){result=d1*d2;}else if (option.equals("/")){if (d2==0){result=0;}else {result = d1 / d2*1.0;}}if (!s1.contains(".")&&!s2.contains(".")){int r=(int) result;tv_label.setText(Integer.toString(r)+"");}else{tv_label.setText(Double.toString(result) +"");}}else if (!s1.equals("")&&s2.equals("")) {           //s1不为空,s2为空double d1=Double.parseDouble(s1);if (option.equals("+")) {result = d1;}else if (option.equals("-")){result=d1;}else if (option.equals("*")){result=0;}else if (option.equals("/")){result=0;}if (!s1.contains(".")){int r=(int) result;tv_label.setText(r+"");}else{tv_label.setText(result+"");}}else if (s1.equals("")&&!s2.equals("")) {           //s1为空,s2不为空double d2=Double.parseDouble(s2);if (option.equals("+")) {result = d2;}else if (option.equals("-")){result=-d2;}else if (option.equals("*")){result=0;}else if (option.equals("/")){result=0;}if (!s2.contains(".")){int r=(int) result;tv_label.setText(r+"");}else{tv_label.setText(result+"");}}else{tv_label.setText("");}}
}

用android studio 完成简单计算器设计开发日志相关推荐

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

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

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

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

  3. Android Studio完成简单UI设计

    目录 一.五个简单UI设计: a.双击打开activity main.xml b.添加辅助线 c.添加按钮输入框: 二.TextView图文混排 三.修改不同颜色的按钮 四.设计CheckBox和ra ...

  4. Android Studio实现简单计算器

    代码: activity_3.xml <?xml version="1.0" encoding="utf-8"?> <androidx.con ...

  5. github android 计算器,Android studio实现简单的计算器

    本文实例为大家分享了Android studio实现简单计算器的具体代码,供大家参考,具体内容如下 需求分析及概要设计 目的 开发一个简单的计算器App,使之能够完成加减乘除混合运算 工具及环境 使用 ...

  6. Android实现有声计算器代码,android studio实现简单的计算器(无bug)

    本文实例为大家分享了android studio实现简单计算器的具体代码,供大家参考,具体内容如下 1.效果图 2布局代码 3.逻辑代码 package com.example.myzhuoye; i ...

  7. APP开发流程实例讲解-儒释道网络电台八天开发全程-在Android Studio中完成界面设计

    APP开发流程实例讲解-儒释道网络电台八天开发全程 功能和界面初步设定 APP开发流程实例讲解-儒释道网络电台八天开发全程 项目发起 功能和界面初步设定 在Android Studio中完成界面设计 ...

  8. 使用Android Studio实现简单的计算器(已亲自实验通过)

    使用Android Studio实现简单的计算器(已亲自实验通过) 实验内容 用多个Activity实现分别实现加.减.乘.除的计算器,具体包括:通过一个启动Activity布置4个按钮,每个按钮分别 ...

  9. 用Android Studio 编写简易计算器

    用Android Studio 编写简易计算器 界面设计 功能的實現 其他配置 1.頂部標題 2.修改測試 这是用Android Studio编写的 简易计算器 .功能主要是实现简单的加减操作,以及比 ...

最新文章

  1. php中关于mysqli和mysql区别
  2. java try-with-resource语句使用
  3. JavaScript学习笔记——underscore操作对象的方法
  4. sql server 2008学习1–系统数据库
  5. 磁盘的顺序读写与随机读写详解
  6. Jq获取同一名称单选框(radio)被选中的值
  7. The superclass javax.servlet.http.HttpServlet was not found on the Java Build
  8. mpAndroidchart 坐标和图表距离_【玩转图表系列】六步,美化你的图表,让老板刮目相看!...
  9. redis和mysql数据不一致_高并发下为什么 redis 和数据库不一致?怎么解决?
  10. Win11任务栏图标重叠怎么办 Win11任务栏图标重叠的解决方法
  11. vb把窗体嵌入桌面底层_桌面透明便签插件便签软件
  12. 五子棋java百度贴吧_五子棋单机版
  13. 开课吧课堂:Kubernetes集群环境常见问题解决
  14. 结对项目---万年历
  15. 【期刊会议】机器学习深度学习 + 计算机视觉(To be continued..)
  16. python预处理tif格式图片
  17. 泰坦尼克号比赛大神级分析
  18. 大地坐标系、地理坐标系、投影坐标系
  19. 计算机网安全模式,电脑安全模式有什么用?
  20. web防火墙和waf防火墙的区别

热门文章

  1. 弹性理论法研究桩基受力计算公式_桩基计算理论
  2. pg_dump实例详解
  3. SQL的理论基础-关系代数
  4. lapack 安装错误
  5. Mac下载Halcon
  6. zepto知识点总结
  7. java ftpclient quit_ftp中ftpClient类的API
  8. (三)随处可见的LED广告屏是怎么工作的呢?接入GUI
  9. Ai-WB2系列模组linux开发环境搭建
  10. 一款最优秀、好用的解压神器