Android 简单的计算器实现

时间:2022-12-29 14:58:02

android中实现一个简单的计算器,实现连续的加减乘除运算。
首先,让我们看下布局:
Android 简单的计算器实现

最上边是一个edittext 用来显示用户输入的运算数字和运算符,下面是一个TableLayout显示数字和运算符按钮。
话不多说,主要代码如下:

package com.easygoing.androidcalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button bt_num0;
    private Button bt_num1;
    private Button bt_num2;
    private Button bt_num3;
    private Button bt_num4;
    private Button bt_num5;
    private Button bt_num6;
    private Button bt_num7;
    private Button bt_num8;
    private Button bt_num9;
    private Button bt_clear;
    private Button bt_percent;
    private Button bt_delete;
    private Button bt_div;
    private Button bt_mult;
    private Button bt_minus;
    private Button bt_plus;
    private Button bt_equal;
    private Button bt_dot;


    private EditText et_calc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViewAndListener();

    }

    private void initViewAndListener() {
        bt_num0 = findViewById(R.id.bt_num0);
        bt_num0.setOnClickListener(this);

        bt_num1 = findViewById(R.id.bt_num1);
        bt_num1.setOnClickListener(this);


        bt_num2 = findViewById(R.id.bt_num2);
        bt_num2.setOnClickListener(this);

        bt_num3 = findViewById(R.id.bt_num3);
        bt_num3.setOnClickListener(this);

        bt_num4 = findViewById(R.id.bt_num4);
        bt_num4.setOnClickListener(this);

        bt_num5 = findViewById(R.id.bt_num5);
        bt_num5.setOnClickListener(this);

        bt_num6 = findViewById(R.id.bt_num6);
        bt_num6.setOnClickListener(this);

        bt_num7 = findViewById(R.id.bt_num7);
        bt_num7.setOnClickListener(this);

        bt_num8 = findViewById(R.id.bt_num8);
        bt_num8.setOnClickListener(this);

        bt_num9 = findViewById(R.id.bt_num9);
        bt_num9.setOnClickListener(this);


        bt_clear = findViewById(R.id.bt_clear);
        bt_clear.setOnClickListener(this);

        bt_percent = findViewById(R.id.bt_percent);
        bt_percent.setOnClickListener(this);

        bt_delete = findViewById(R.id.bt_delete);
        bt_delete.setOnClickListener(this);

        bt_div = findViewById(R.id.bt_div);
        bt_div.setOnClickListener(this);

        bt_mult = findViewById(R.id.bt_mult);
        bt_mult.setOnClickListener(this);

        bt_minus = findViewById(R.id.bt_minus);
        bt_minus.setOnClickListener(this);


        bt_plus = findViewById(R.id.bt_plus);
        bt_plus.setOnClickListener(this);

        bt_equal = findViewById(R.id.bt_equal);
        bt_equal.setOnClickListener(this);


        et_calc = findViewById(R.id.et_calc);

        bt_dot = findViewById(R.id.bt_dot);
        bt_dot.setOnClickListener(this);

    }


    private boolean lastIsOperator;
    private String lastOperators = "";
    private List<String> operatorNumbers = new ArrayList<>();
    private List<String> operators = new ArrayList<>();;

    private double firstNumber = 0D;
    private double sencondNumber = 0D;

    @Override
    public void onClick(View v) {

        String currentText = et_calc.getText().toString();

        String operatorNumber = "";

        if (currentText.equals("0")) {
            et_calc.setText("");
        }


        operatorNumber = et_calc.getText().toString();

        if(!lastOperators.equals(""))
        {
            int index = operatorNumber.lastIndexOf(lastOperators);
            operatorNumber = operatorNumber.substring(index+1);
        }




        switch (v.getId()) {
            case R.id.bt_num0:
                et_calc.setText(et_calc.getText() + "0");
                lastIsOperator = false;
                break;
            case R.id.bt_num1:
                et_calc.setText(et_calc.getText() + "1");
                lastIsOperator = false;
                break;
            case R.id.bt_num2:
                et_calc.setText(et_calc.getText() + "2");
                lastIsOperator = false;
                break;
            case R.id.bt_num3:
                et_calc.setText(et_calc.getText() + "3");
                lastIsOperator = false;
                break;
            case R.id.bt_num4:
                et_calc.setText(et_calc.getText() + "4");
                lastIsOperator = false;
                break;
            case R.id.bt_num5:
                et_calc.setText(et_calc.getText() + "5");
                lastIsOperator = false;
                break;
            case R.id.bt_num6:
                et_calc.setText(et_calc.getText() + "6");
                lastIsOperator = false;
                break;
            case R.id.bt_num7:
                et_calc.setText(et_calc.getText() + "7");
                lastIsOperator = false;
                break;
            case R.id.bt_num8:
                et_calc.setText(et_calc.getText() + "8");
                lastIsOperator = false;
                break;
            case R.id.bt_num9:
                et_calc.setText(et_calc.getText() + "9");
                lastIsOperator = false;
                break;
            case R.id.bt_dot:
                et_calc.setText(et_calc.getText() + ".");
                lastIsOperator = false;
                break;
            case R.id.bt_clear:
                et_calc.setText("");
                lastIsOperator = false;
                firstNumber = 0D;
                sencondNumber = 0D;
                lastOperators ="=";
                break;
            case R.id.bt_percent:
                lastIsOperator = true;
                break;
            case R.id.bt_delete:
                if (TextUtils.isEmpty(et_calc.getText())) {
                    return;
                }
                lastIsOperator = false;
                et_calc.setText(currentText.substring(0, currentText.length() - 1).length() > 0 ? currentText.substring(0, currentText.length() - 1) : "0");
                break;
            case R.id.bt_div:

                if ((TextUtils.isEmpty(et_calc.getText())
                        || lastIsOperator) && !lastOperators.equals("=")) {
                    return;
                }
                opratorCalc(operatorNumber,"÷");
                lastIsOperator = true;
                et_calc.setText(et_calc.getText() + "÷");
                lastOperators = "÷";
                break;
            case R.id.bt_mult:
                if ((TextUtils.isEmpty(et_calc.getText())
                        || lastIsOperator) && !lastOperators.equals("=")) {
                    return;
                }
                opratorCalc(operatorNumber,"*");
                lastIsOperator = true;
                et_calc.setText(et_calc.getText() + "*");
                lastOperators = "*";
                break;
            case R.id.bt_minus:
                if ((TextUtils.isEmpty(et_calc.getText())
                        || lastIsOperator) && !lastOperators.equals("=")) {
                    return;
                }
                opratorCalc(operatorNumber,"-");
                lastIsOperator = true;
                et_calc.setText(et_calc.getText() + "-");
                lastOperators = "-";
                break;
            case R.id.bt_plus:
                if ((TextUtils.isEmpty(et_calc.getText())
                        || lastIsOperator) && !lastOperators.equals("=")) {
                    return;
                }
                opratorCalc(operatorNumber,"+");
                lastIsOperator = true;
                et_calc.setText(et_calc.getText() + "+");
                lastOperators = "+";
                break;
            case R.id.bt_equal:
                double result = 0D;
                if(TextUtils.isEmpty(lastOperators))
                {
                    return;
                }
                opratorResult(operatorNumber);
                sencondNumber = 0D;
                lastOperators ="=";
                lastIsOperator = true;
                et_calc.setText(et_calc.getText() + "\n=" + String.valueOf(firstNumber));
                break;
        }
    }

    private void operate(String operatorNumber)
    {
        if(sencondNumber != 0D)
        {
            if(lastOperators.equals("÷"))
            {
                sencondNumber = sencondNumber / Double.parseDouble(operatorNumber);
                firstNumber = firstNumber / sencondNumber;
            }
            else if(lastOperators.equals("*"))
            {
                sencondNumber = sencondNumber * Double.parseDouble(operatorNumber);
                firstNumber = firstNumber * sencondNumber;
            }
            else if(lastOperators.equals("+"))
            {
                sencondNumber = Double.parseDouble(operatorNumber);
                firstNumber = firstNumber + sencondNumber;
            }
            else if(lastOperators.equals("-"))
            {
                sencondNumber = Double.parseDouble(operatorNumber);
                firstNumber = firstNumber - sencondNumber;
            }
        }
        else
        {
            if(lastOperators.equals("÷"))
            {
                firstNumber = firstNumber / Double.parseDouble(operatorNumber);
            }
            else if(lastOperators.equals("*"))
            {
                firstNumber = firstNumber * Double.parseDouble(operatorNumber);
            }
            else if(lastOperators.equals("+"))
            {
                firstNumber = firstNumber + Double.parseDouble(operatorNumber);
            }
            else if(lastOperators.equals("-"))
            {
                firstNumber= firstNumber - Double.parseDouble(operatorNumber);
            }


        }
    }
    public void opratorResult(String operatorNumber)
    {
        operate(operatorNumber);

    }
    public void opratorCalc(String operatorNumber,String currentOprator)
    {
        if(TextUtils.isEmpty(lastOperators))
        {
            firstNumber = Double.parseDouble(operatorNumber);
            return;
        }

        if(lastOperators.equals(currentOprator))
        {
            if(lastOperators.equals("÷"))
            {
                firstNumber = firstNumber / Double.parseDouble(operatorNumber);
            }
            else if(lastOperators.equals("*"))
            {
                firstNumber = firstNumber * Double.parseDouble(operatorNumber);
            }
            else if(lastOperators.equals("+"))
            {
                firstNumber = firstNumber + Double.parseDouble(operatorNumber);
            }
            else if(lastOperators.equals("-"))
            {
                firstNumber = firstNumber - Double.parseDouble(operatorNumber);
            }

            return;
        }
        operate(operatorNumber);

    }

}
```**重点内容**

至于button的selector样式比较简单,这里简单贴出来两个:、





<div class="se-preview-section-delimiter"></div>

普通button样式

<?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="com.easygoing.androidcalculator.MainActivity">

    <EditText  android:id="@+id/et_calc" style="@style/et_calc_style" />

    <TableLayout  android:layout_width="match_parent" android:layout_height="wrap_content">
        <TableRow>
            <Button  android:id="@+id/bt_clear" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="C"/>
            <Button  android:id="@+id/bt_percent" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="+/-"/>

            <Button  android:id="@+id/bt_delete" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="Del" />
            <Button  android:id="@+id/bt_div" style="@style/bt_styles_common" android:background="@drawable/btn_operate_background" android:textColor="#FFFFFF" android:text="÷"/>
        </TableRow>

        <TableRow>
            <Button  android:id="@+id/bt_num7" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="7"/>
            <Button  android:id="@+id/bt_num8" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="8"/>
            <Button  android:id="@+id/bt_num9" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="9"/>
            <Button  android:id="@+id/bt_mult" style="@style/bt_styles_common" android:background="@drawable/btn_operate_background" android:textColor="#FFFFFF" android:text="×"/>
        </TableRow>

        <TableRow>
            <Button  android:id="@+id/bt_num4" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="4"/>
            <Button  android:id="@+id/bt_num5" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="5"/>
            <Button  android:id="@+id/bt_num6" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="6"/>
            <Button  android:id="@+id/bt_minus" style="@style/bt_styles_common" android:background="@drawable/btn_operate_background" android:textColor="#FFFFFF" android:text="-"/>
        </TableRow>
        <TableRow>
            <Button  android:id="@+id/bt_num1" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="1"/>
            <Button  android:id="@+id/bt_num2" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="2"/>
            <Button  android:id="@+id/bt_num3" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="3"/>
            <Button  android:id="@+id/bt_plus" style="@style/bt_styles_common" android:background="@drawable/btn_operate_background" android:textColor="#FFFFFF" android:text="+"/>
        </TableRow>
        <TableRow>
            <Button  android:id="@+id/bt_num0" style="@style/bt_styles_common" android:background="@drawable/btn_background" android:text="0"/>
            <Button  android:id="@+id/bt_dot" style="@style/bt_styles_common" android:layout_weight="0.5" android:background="@drawable/btn_background" android:text="·"/>
            <Button  android:id="@+id/bt_equal" style="@style/bt_styles_common" android:layout_weight="0.5" android:background="@drawable/btn_operate_background" android:textColor="#FFFFFF" android:text="="/>
        </TableRow>
    </TableLayout>
</LinearLayout>

这里需要注意的一点就是再按钮0的布局那里,需要设置一样权重,让按钮0能够占两个按钮的长度,

代码的逻辑比较简单,主要是记录三个操作数和两个运算符,当有两个运算符的时候,比较运算符的优先级,先行运算,然后保存计算结果,详细的可以看下代码。

当然了,这个计算器的功能不是很完善,这里只不过是进行一个简单的示例,有兴趣的可以自己做一个。。

源代码下载地址:http://download.csdn.net/download/qingyulove/10181313