Android-计算器的实现

作者: 3a7e0b22a5df | 来源:发表于2017-08-12 15:27 被阅读1023次

    简介

    计算器的应用在我们的日常生活中十分普遍,对于对安卓开发有兴趣的朋友,了解并掌握它的开发是很重要的。对于入门新手来说,此小项目也是非常有用的,一是熟悉开发工具的使用,二是逐步入门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的初学者,很多问题还不懂,写下这些,对一部分读者有用自然是好,更多地是想记录下自己学习的过程,逐步提高自己,当然,有不好的地方,非常欢迎高手指点批评,非常欢迎相互讨论,相互提高!
    今天到这里,谢谢!

    相关文章

      网友评论

        本文标题:Android-计算器的实现

        本文链接:https://www.haomeiwen.com/subject/ofhjrxtx.html