Android 简单计算机的实现

新建工程

首先是创建一个新的工程

android 计算 android计算机实现_实例化


新建步骤如下:

android 计算 android计算机实现_xml_02


点击next后

android 计算 android计算机实现_xml_03


点击Next

android 计算 android计算机实现_android 计算_04


选择Empty Activity选项

android 计算 android计算机实现_android_05


将第一个默认勾选的选项取消勾选,此选项是创建一个默认layout文件,因为我们要使用自己的layout文件作为布局文件,所以我们把此选项改为不勾选,最后点击finish完成创建。

新建文件错误解决

android 计算 android计算机实现_实例化_06


新建完成后文件报错,报错原因是build.grade文件中的版本不符所造成的问题,进行如下修改:

打开build.grade的文件:

android 计算 android计算机实现_android 计算_07


将其中的版本号30改为26

android 计算 android计算机实现_android 计算_08


如上图所示,修改完成后点击Try again重新加载,错误即可解决。

android 计算 android计算机实现_android_09

代码实现

我们要实现代码首先需要一个写代码的文件,首先创建一个布局代码的容器即在res文件夹下新建一个directory文件并将其命名为layout

android 计算 android计算机实现_android studio_10


接着在layout文件夹下新建一个布局文档(Layout resource file)用于存放布局代码,示例:将文件名命名为main

android 计算 android计算机实现_android studio_11


android 计算 android计算机实现_android 计算_12

布局代码

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

    <TextView
        android:id="@+id/textResult" //命名id是为后面计算器功能的实现做准备
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:gravity="right" //设置文本对齐方式为左对齐
        android:text="" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"> //表示该布局水平对齐

        <Button  //按钮
            android:id="@+id/btnCLs" //为设置监听做准备
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" //android:layout_weight用于给一个线性布局中的诸多视图的重要度赋值
            android:text="C"/>
        <Button
            android:id="@+id/btnDel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Del"/>
        <Button
            android:id="@+id/btnChu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="÷"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnOne"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
        <Button
            android:id="@+id/btnTwo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"/>
        <Button
            android:id="@+id/btnThree"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"/>
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnFour"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4"/>
        <Button
            android:id="@+id/btnFive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5"/>
        <Button
            android:id="@+id/btnSix"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"/>
        <Button
            android:id="@+id/btnReduce"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnSeven"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7"/>
        <Button
            android:id="@+id/btnEight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8"/>
        <Button
            android:id="@+id/btnNine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"/>
        <Button
            android:id="@+id/btnMul"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="*"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnZero"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="0"/>
        <Button
            android:id="@+id/btnPoint"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="."/>
        <Button
            android:id="@+id/btnEquals"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="="/>

    </LinearLayout>
</LinearLayout>

用LinearLayout 包裹 LinearLayout 进行布局便于分层管理,通过LinearLayout线性布局最终布局效果图如下:

android 计算 android计算机实现_android studio_13

功能代码

package com.example.lenovo.calculator;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{//实现鼠标点击的事件的监听接口

    //创建每个按钮的对象
    //数字按钮
    Button btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine,btnZero;
    //加减乘除
    Button btnChu,btnAdd,btnReduce,btnMul;
    //其他按钮
    Button btnCLs,btnDel,btnEquals,btnPoint;
    //文本框
    TextView textResult;

    //计算完成后,我们应将TextView的文本值再次设置为空,否则将会影响下一次的数据输入
    //因此我们需要一个标签进行判断是否是第一次计算已完成
    boolean flag;//boolean值默认为FALSE所以当我们第一次进行运算时,不会对计算产生影响


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//设置采用哪一个布局文件

        //实例化每个按钮 通过每个按钮的id进行实例化创建
        btnOne = (Button) findViewById(R.id.btnOne);
        btnTwo = (Button) findViewById(R.id.btnTwo);
        btnThree = (Button) findViewById(R.id.btnThree);
        btnFour = (Button) findViewById(R.id.btnFour);
        btnFive = (Button) findViewById(R.id.btnFive);
        btnSix = (Button) findViewById(R.id.btnSix);
        btnSeven = (Button) findViewById(R.id.btnSeven);
        btnEight = (Button) findViewById(R.id.btnEight);
        btnNine = (Button) findViewById(R.id.btnNine);
        btnZero = (Button) findViewById(R.id.btnZero);

        btnChu = (Button) findViewById(R.id.btnChu);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnReduce = (Button) findViewById(R.id.btnReduce);
        btnMul = (Button) findViewById(R.id.btnMul);

        btnCLs = (Button) findViewById(R.id.btnCLs);
        btnDel = (Button) findViewById(R.id.btnDel);
        btnEquals = (Button) findViewById(R.id.btnEquals);
        btnPoint = (Button) findViewById(R.id.btnPoint);

        textResult = (TextView)findViewById(R.id.textResult);

        //设置每个按钮的点击事件即监听按钮
        btnOne.setOnClickListener(this);
        btnTwo.setOnClickListener(this);
        btnThree.setOnClickListener(this);
        btnFour.setOnClickListener(this);
        btnFive.setOnClickListener(this);
        btnSix.setOnClickListener(this);
        btnSeven.setOnClickListener(this);
        btnEight.setOnClickListener(this);
        btnNine.setOnClickListener(this);
        btnZero.setOnClickListener(this);

        btnChu.setOnClickListener(this);
        btnAdd.setOnClickListener(this);
        btnReduce.setOnClickListener(this);
        btnMul.setOnClickListener(this);

        btnCLs.setOnClickListener(this);
        btnDel.setOnClickListener(this);
        btnEquals.setOnClickListener(this);
        btnPoint.setOnClickListener(this);
    }

    //重写其中的
    @Override
    public void onClick(View view) {

        String str = textResult.getText().toString(); //获取当前textview文本
        switch (view.getId()) {
            case R.id.btnOne:
            case R.id.btnTwo:
            case R.id.btnThree:
            case R.id.btnFour:
            case R.id.btnFive:
            case R.id.btnSix:
            case R.id.btnSeven:
            case R.id.btnEight:
            case R.id.btnNine:
            case R.id.btnZero:
            case R.id.btnPoint:
                //如果flag为TRUE说明已完成一次计算,因此我们就需要将其恢复为FALSE,将文本和字符串str恢复为默认值
                if(flag){
                    flag = false;
                    str = "";
                    textResult.setText("");

                }
                textResult.setText(str + ((Button) view).getText());
                break;

            case R.id.btnChu:
            case R.id.btnAdd:
            case R.id.btnReduce:
            case R.id.btnMul:
                if(flag){
                    flag = false;
                    str = "";
                    textResult.setText("");

                }
                textResult.setText(str + " " + ((Button) view).getText() + " ");
                break;

            case R.id.btnCLs:
                if(flag){
                    flag = false;
                    str = "";
                    textResult.setText("");

                }
                textResult.setText("");
                break;

            case R.id.btnDel:
                if(flag){
                    flag = false;
                    str = "";
                    textResult.setText("");

                }

                if(str != null && !str.equals("")){
                    textResult.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.btnEquals:
                getresult();
        }
    }

    private void getresult() {
        String exp = textResult.getText().toString(); //从textview获取整个文本
        if(exp == null || exp.equals("")){
            return;
        }
        if(!exp.contains(" ")){
            return;
        }
        //在getresult方法内因为没有对str字符串和文本框做出任何修改所以我们只需要将flag的值转变为FALSE即可
        if(flag){
            flag = false;
            return;
        }

        //进入getresult方法代表第一次求值,完成后flag = TRUE
        flag = true;
        //将其中的两个数字和运算符分割出来
        float result = 0;
        String num1 = exp.substring(0,exp.indexOf(" "));
        String ex = exp.substring(exp.indexOf(" ") + 1,exp.indexOf(" ") + 2);
        String num2 = exp.substring(exp.indexOf(" ") + 3);

        //如果两个数字都不为空则判断运算符进行运算
        if(!num1.equals("") && !num2.equals("")) {
            float d1 = Float.parseFloat(num1);
            float d2 = Float.parseFloat(num2);

            if (ex.equals("+")) {
                result = d1 + d2;
            } else if (ex.equals("-")) {
                result = d1 - d2;
            } else if (ex.equals("*")) {
                result = d1 * d2;
            } else if (ex.equals("÷")) {
                if (d2 == 0) {
                    result = 0;
                } else {
                    result = d1 / d2;
                }
            }
            textResult.setText(result + "");
        }else if(!num1.equals("") && num2.equals("")){
            textResult.setText(exp.substring(0,exp.indexOf(" ")));
        }else if(num1.equals("") && !num2.equals("")){
            float d3 = Float.parseFloat(num2);
            if(ex.equals("+")){
                result = 0 + d3;
            }else if(ex.equals("-")){
                result = 0 - d3;
            }else if(ex.equals("*")){
                result = 0 * d3;
            }else if(ex.equals("÷")){
                result = 0;
            }
            textResult.setText(result + "");
        }else{
            textResult.setText("");
        }
    }
}

这段代码的理解难点在于flag的使用,我们理解的时候可以先将关于flag的相关内容删除后进行第一遍运行理解,第一遍运行后,第二遍运行时,会发现原来的的出来的结果并没有消失,反而会将你输入的结果与原来的结果拼接到一起,而我们的简单计算机原理为将两个数通过“ ”进行分割判断求解,所以两个结果拼接后,就无法进行计算。

Over!
Over!