Android-计算器的实现

简介

计算器的应用在我们的日常生活中十分普遍,对于对安卓开发有兴趣的朋友,了解并掌握它的开发是很重要的。对于入门新手来说,此小项目也是非常有用的,一是熟悉开发工具的使用,二是逐步入门Android开发。接下来我将逐步总结一下该应用的实现过程。

开发工具

Android Studio

开发环境

1)JDK(Java Development Kit)JDK是Java语言的软件开发工具包,主要用于移动设备、嵌入设备上的java应用程序。

2)SDK(software development kit) SDK是软件开发工具包。 被软件开发工程师用于为特定的软件包、软件框架、硬件平台、操作系统等建立应用软件的开发工具的集合

实现过程

Android自带的硬件类Camera在API21(5.0)之后被抛弃了,出现了Camera2,在这可以用CameraManager来打开,实现硬件闪光灯的开启与关闭。

实现过程

分析

我认为每个开发android应用前,我们肯定是考虑UI设计与后台的逻辑处理这俩部分。在UI设计中,要注意到程序的美观以及可行性,后台对相应的操作进行处理。

1.新建项目

使用Android Studio新建一个空项目。

2.添加布局

在这里,我运用的是嵌套的线性布局,布局中再放置按钮。UI是之前刚学Android做的,为了方便没有去更改,其实可以运用TableLayout来做,布局会更美观。
效果图如下:


image.png

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="right"
        android:editable="false" />
    //设置文本框不能编辑
    //match parent 充满父类容器

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
       >
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_clear"
        android:layout_margin="8dp"
        android:text="C"
        android:textSize="20dp"
        />
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_del"
        android:layout_margin="8dp"
        android:text="DEL"
        android:textSize="20dp"
    />
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_divide"
        android:layout_margin="8dp"
        android:text="÷"
        android:textSize="20dp"
       />
    <Button
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/btn_multipy"
        android:layout_margin="8dp"
        android:text="×"
        android:textSize="20dp"
         />
        </LinearLayout>

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_7"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:text="7"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_8"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="8"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_9"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="9"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_minus"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="-"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
    >
        <Button
            android:id="@+id/btn_4"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:text="4"
            android:layout_margin="8dp"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_5"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="5"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_6"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="6"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_plus"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="+"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_gravity="left"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/btn_1"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:text="1"
            android:layout_margin="8dp"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="2"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_3"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="10dp"
            android:text="3"
            android:textSize="20dp" />
        <Button
            android:id="@+id/btn_a"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:text="="
            android:layout_margin="8dp"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="left"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_0"
            android:layout_width="166dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="10dp"
            android:text="0"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btn_b"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="."
            android:textSize="20dp" />

        <Button
            android:id="@+id/button"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="right"
            android:layout_margin="8dp"
            android:text="空"
            android:textSize="20dp" />

    </LinearLayout>

</LinearLayout>

3.MainActivity的编写

这部分的话就是对各个按钮点击事件的处理了,也就是对键入的数字进行运算,判断之类的,由于是出学者,我只是做了简单的处理,还有一些BUG没能处理好,当然,不影响简单运算的使用。
代码如下:

package com.zewei_w.caculator;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {
    Button btn_1;  Button btn_0;
    Button btn_2;  Button btn_7;
    Button btn_3;  Button btn_8;  Button btn_plus;
    Button btn_4;  Button btn_9;  Button btn_minus;
    Button btn_5;  Button btn_a;  Button btn_multipy;
    Button btn_divide;  Button btn_del;
    Button btn_6;  Button btn_b;
    Button btn_clear;
    EditText et_input;
    boolean clear_flag;
    //clearflag默认为false
    // 定义这些按钮和文本框
    //通过接口方式实现事件的监听
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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_9=(Button) findViewById(R.id.btn_9);
        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_clear=(Button) findViewById(R.id.btn_clear);
        btn_del=(Button) findViewById(R.id.btn_del);
        btn_divide=(Button) findViewById(R.id.btn_divide);
        btn_multipy=(Button) findViewById(R.id.btn_multipy);
        btn_minus=(Button) findViewById(R.id.btn_minus);
        btn_plus=(Button) findViewById(R.id.btn_plus);
        btn_a=(Button) findViewById(R.id.btn_a);
        btn_b=(Button) findViewById(R.id.btn_b);
        //实例化按钮
        // 返回的是view类型,需要转换成button类型
        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_a.setOnClickListener(this);
        btn_b.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_multipy.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        //设置按钮的点击事件
        //大概是获取点击事件
    }
    @Override
    public 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_b:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText(str + ((Button) v).getText());
                break;
            case R.id.btn_plus:
            case R.id.btn_multipy:
            case R.id.btn_divide:
            case R.id.btn_minus:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                //判断用
                et_input.setText(str + " " + ((Button) v).getText() + " ");
                break;
            case R.id.btn_clear:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                et_input.setText("");
                break;
            case R.id.btn_del:
                if(clear_flag)
                {
                    clear_flag=false;
                    str="";
                    et_input.setText("");
                }
                if (str != null && !str.equals("")) {
                    et_input.setText(str.substring(0, str.length() - 1));
                }
                break;
            case R.id.btn_a:
                getresult();
                break;
        }
    }
        private void getresult()
    {  String exp=et_input.getText().toString();
        if(exp==null||exp.equals(""))
        {
            return;
        }
        if(!exp.contains(" "))
        {
            return;
        }
        if(clear_flag)
        {
            clear_flag=false;
            return;
        }
             clear_flag=true;
              double result=0;
             String s1=exp.substring(0,exp.indexOf(" "));
             String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
             String s2=exp.substring(exp.indexOf(" ")+3);
        if(!s1.equals("")&&!s2.equals("")) {
            double d1= Double.parseDouble(s1);
            double d2 = Double.parseDouble(s2);
            //强制类型转换

            if (op.equals("+")) {
                result = d1 + d2;
            } else if (op.equals("-")) {
                 result = d1-d2;
            } else if (op.equals("×")) {
                result=d1*d2;
            } else if (op.equals("÷")) {
            if(d1==0)
            {result=0;}else  result=d1/d2;
            }
             if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
                int r = (int)result;
                 et_input.setText(r + "");
             }
              else
                 {et_input.setText(result + "");}
        }else if(!s1.equals("")&&s2.equals(""))
        {
            et_input.setText(exp);
        }else if(s1.equals("")&&!s2.equals(""))
        {   double d2= Double.parseDouble(s2);
            if (op.equals("+")) {
                result = 0 + d2;
            } else if (op.equals("-")) {
                result = 0 - d2;
            } else if (op.equals("×")) {
                result=0*d2;
            } else if (op.equals("÷")) {
               result=0;
            }
        }
        else {
            et_input.setText("");
        }
    }
        }

代码中有适当的注释说明。

4.运行程序

运行程序,调试错误,可更改APP名字,图标等
这里我在AndroidMa'nifest.xml文件中更改了主题
效果如果:
(编写手机截图)


可拓展指出:

1.程序界面美化
2.功能完善化-检查BUG
3.增加运算功能

个人总结与感悟:

这个应用同手电筒一样,都是入门级的,但是我仍旧极力推荐,对于初学者非常有用,在编写过程中,学到了不少知识点,同时我发现,随着Android的不断更新,很多接口以及写法都有些许改变,所以我认为,遇到了问题,查找百度、谷歌等固然可以,但一定不能照搬,因为很多问题其实是很久之前的,而很多知识的用法却一直在更新改变,所以要渐渐地学会去研读开发文档,找到用法,这点接下来我会开始去做,因为确实有很多的不同,比如在主题的运用这点,因为用法问题,我就被干扰了不久。
编者刚开始写简书不久,也是Android的初学者,很多问题还不懂,写下这些,对一部分读者有用自然是好,更多地是想记录下自己学习的过程,逐步提高自己,当然,有不好的地方,非常欢迎高手指点批评,非常欢迎相互讨论,相互提高!
今天到这里,谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,050评论 25 707
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,658评论 22 664
  • (这是九瓣 365日写作计划第1天的写作内容。) 内供的苦恼其实是来自被鼻子刺伤的自尊心。 芥川的《鼻》中内供鼻子...
    九瓣阅读 824评论 2 4
  • 春暖花开,四月樱花季。喜欢拍花花草草,大路小路处处有惊喜,迎春花,玉兰花,樱花,杜鹃花,郁金香…黄色白色粉色红色…...
    hiElena阅读 171评论 0 0
  • 1.HTTP 1.1全局规范 URLURL的组成:基本的网络地址 + 分支节点http://172.19.201....
    zlcook阅读 883评论 0 0