Bootstrap

Android 简易计算器

功能实现加、减、乘、除、连续计算等简单运算。

调试结果如下图:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/shape_radius2"
    tools:context="com.wu.mytest.CraActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="MissingConstraints"
        android:background="@drawable/main_radius2"
        android:id="@+id/layout2"
        >

        <TextView
            android:id="@+id/result1"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_marginTop="0dp"

            android:gravity="right|bottom"
            android:text="0"
            android:textColor="#F8FFF1"
            android:textSize="60sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <TextView
            android:id="@+id/result2"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:gravity="right|bottom"
            android:text="0"
            android:textColor="#9FCF66"
            android:textSize="60sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/result1" />
    </LinearLayout>

    <Button
        android:id="@+id/button_back"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="→"
        android:textColor="#ff0000"
        android:textSize="35dp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button_divide"
        app:layout_constraintTop_toBottomOf="@+id/layout2" />

    <Button
        android:id="@+id/button_divide"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_below="@+id/layout2"
        android:background="@drawable/shape_screen"
        android:text="/"
        android:textSize="34sp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/button_back"
        app:layout_constraintRight_toLeftOf="@id/button_multiply"
        app:layout_constraintTop_toBottomOf="@+id/layout2" />

    <Button
        android:id="@+id/button_multiply"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="*"
        android:textSize="34sp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/button_divide"
        app:layout_constraintRight_toLeftOf="@id/button_clear"
        app:layout_constraintTop_toBottomOf="@+id/layout2" />

    <Button
        android:id="@+id/button_clear"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="C"
        android:textSize="34sp"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@id/button_multiply"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout2" />

    <Button
        android:id="@+id/button_9"
        android:layout_width="100dp"

        android:layout_height="100dp"
        android:background="@drawable/shape_screen"

        android:text="9"
        android:textSize="35dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button_8"
        app:layout_constraintTop_toBottomOf="@id/button_back" />

    <Button
        android:id="@+id/button_8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"

        android:text="8"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_9"
        app:layout_constraintRight_toLeftOf="@id/button_7"
        app:layout_constraintTop_toBottomOf="@id/button_back" />

    <Button
        android:id="@+id/button_7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"

        android:text="7"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_8"
        app:layout_constraintRight_toLeftOf="@id/button_add"
        app:layout_constraintTop_toBottomOf="@id/button_back" />

    <Button
        android:id="@+id/button_add"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="+"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_7"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button_back" />

    <Button
        android:id="@+id/button_6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="6"
        android:textSize="35dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button_5"
        app:layout_constraintTop_toBottomOf="@id/button_9" />

    <Button
        android:id="@+id/button_5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="5"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_6"
        app:layout_constraintRight_toLeftOf="@id/button_4"
        app:layout_constraintTop_toBottomOf="@id/button_9" />

    <Button
        android:id="@+id/button_4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="4"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_5"
        app:layout_constraintRight_toLeftOf="@id/button_clear"
        app:layout_constraintTop_toBottomOf="@id/button_9" />

    <Button
        android:id="@+id/button_minus"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="-"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button_9" />

    <Button
        android:id="@+id/button_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="3"
        android:textSize="35dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button_2"
        app:layout_constraintTop_toBottomOf="@id/button_6" />

    <Button
        android:id="@+id/button_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="2"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_3"
        app:layout_constraintRight_toLeftOf="@id/button_1"
        app:layout_constraintTop_toBottomOf="@id/button_6" />

    <Button
        android:id="@+id/button_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="1"
        android:textSize="35dp"
        app:layout_constraintLeft_toRightOf="@id/button_2"
        app:layout_constraintRight_toLeftOf="@id/button_dot"
        app:layout_constraintTop_toBottomOf="@id/button_6" />

    <Button
        android:id="@+id/button_dot"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/shape_screen"
        android:text="."
        android:textSize="48sp"
        app:layout_constraintLeft_toRightOf="@id/button_1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button_6" />

    <Button
        android:id="@+id/button_0"
        android:layout_width="200dp"
        android:layout_height="108dp"
        android:background="@drawable/shape_screen"
        android:text="0"
        android:textSize="35dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/button_equal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button_3" />

    <Button
        android:id="@+id/button_equal"
        android:layout_width="200dp"
        android:layout_height="108dp"
        android:layout_marginEnd="4dp"
        android:background="@drawable/shape_screen"
        android:text="="
        android:textSize="35dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toRightOf="@id/button_0"
        app:layout_constraintTop_toBottomOf="@id/button_3" />


</androidx.constraintlayout.widget.ConstraintLayout>

完整代码:CraActivity.java

package com.wu.mytest;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class CraActivity extends AppCompatActivity implements View.OnClickListener{
    //结果
    private TextView Res1,Res2;
    //数字0-9
    private Button but_0,but_1,but_2,but_3,but_4,but_5,but_6,but_7,but_8,but_9;
    //运算符,+,-,×,÷,.,=
    private Button but_clear,but_divide, but_multiply,but_add,but_minus,but_dot,but_equal,but_back;
    private String text = "";//保存输入的数字和符号
    private Double result = 0.0;//输出结果


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contrainlayout);
        //结果
        Res1=(TextView) findViewById(R.id.result1);
        Res2=(TextView) findViewById(R.id.result2);
        //数字0-9
        but_0=(Button)findViewById(R.id.button_0);
        but_1=(Button)findViewById(R.id.button_1);
        but_2=(Button)findViewById(R.id.button_2);
        but_3=(Button)findViewById(R.id.button_3);
        but_4=(Button)findViewById(R.id.button_4);
        but_5=(Button)findViewById(R.id.button_5);
        but_6=(Button)findViewById(R.id.button_6);
        but_7=(Button)findViewById(R.id.button_7);
        but_8=(Button)findViewById(R.id.button_8);
        but_9=(Button)findViewById(R.id.button_9);
        //运算符
        but_add=(Button)findViewById(R.id.button_add);
        but_clear=(Button)findViewById(R.id.button_clear);
        but_divide=(Button)findViewById(R.id.button_divide);
        but_dot=(Button)findViewById(R.id.button_dot);
        but_equal=(Button)findViewById(R.id.button_equal);
        but_minus=(Button)findViewById(R.id.button_minus);
        but_multiply=(Button)findViewById(R.id.button_multiply);
        but_back=(Button)findViewById(R.id.button_back);
        //添加点击事件
        but_0.setOnClickListener((View.OnClickListener) this);
        but_1.setOnClickListener((View.OnClickListener) this);
        but_2.setOnClickListener((View.OnClickListener) this);
        but_3.setOnClickListener((View.OnClickListener) this);
        but_4.setOnClickListener((View.OnClickListener) this);

        but_5.setOnClickListener((View.OnClickListener) this);
        but_6.setOnClickListener((View.OnClickListener) this);
        but_7.setOnClickListener((View.OnClickListener) this);
        but_8.setOnClickListener((View.OnClickListener) this);
        but_9.setOnClickListener((View.OnClickListener) this);

        but_add.setOnClickListener((View.OnClickListener) this);
        but_clear.setOnClickListener((View.OnClickListener) this);
        but_minus.setOnClickListener((View.OnClickListener) this);
        but_divide.setOnClickListener((View.OnClickListener) this);
        but_dot.setOnClickListener((View.OnClickListener) this);

        but_multiply.setOnClickListener((View.OnClickListener) this);
        but_equal.setOnClickListener((View.OnClickListener) this);
        but_back.setOnClickListener((View.OnClickListener) this);
    }

    @Override
    public void onClick(View v) {
        String string=Res1.getText().toString();
        switch (v.getId()){
            case R.id.button_0://0
                num(0);
                break;
            case R.id.button_1://1
                num(1);
                break;
            case R.id.button_2://2
                num(2);
                break;
            case R.id.button_3://3
                num(3);
                break;
            case R.id.button_4://4
                num(4);
                break;
            case R.id.button_5://5
                num(5);
                break;
            case R.id.button_6://6
                num(6);
                break;
            case R.id.button_7://7
                num(7);
                break;
            case R.id.button_8://8
                num(8);
                break;
            case R.id.button_9://9
                num(9);
                break;
            case R.id.button_dot://.
                dot();
                break;
            case R.id.button_add://+
                add();
                break;
            case R.id.button_minus://-
                minus();
                break;
            case R.id.button_multiply://"*"
                multiply();
                break;
            case R.id.button_divide://除
                divide();
                break;
            case R.id.button_clear://清除
                clear();
                break;
            case R.id.button_back://回退一位
                back();
                break;
            //计算结果
            case R.id.button_equal://结果
                result();
                break;
            default:
                break;
        }
        Res1.setText(text);
        Res2.setText(String.valueOf(result));
    }
    private void num(int i) {
        text = text + String.valueOf(i);
    }

    private void dot() {
        int a=text.length();

        String s01="+";
        String s02="-";
        String s03="×";
        String s04="÷";
        String s05=".";
        if (a!=0) {
            String s=text.substring(a-1,a);
            String s0=text.substring(0,a-1);
            if (s01.equals(s) || s02.equals(s) || s03.equals(s) || s04.equals(s) || s05.equals(s)) {
                text = s0+".";
            }else {
                text += ".";
            }
        } else {
            text = "";
            result =0.0;
            Res1.setText("");
            Res2.setText("");
        }
    }

    private void clear() {

        text = "";
        result =0.0;
        Res1.setText("");
        Res2.setText("");

    }

    private void back() {
        if (text.length()!=0){
            String str = text.substring(0, text.length()-1);
            text = str;
        }


    }

    private void add() {
        int a=text.length();

        String s01="+";
        String s02="-";
        String s03="×";
        String s04="÷";
        String s05=".";
        if (a!=0) {
            String s=text.substring(a-1,a);
            String s0=text.substring(0,a-1);
            if (s01.equals(s) || s02.equals(s) || s03.equals(s) || s04.equals(s) || s05.equals(s)) {
                text = s0+"+";
            }else {
                text += "+";
            }
        } else {
            text = "";
            result =0.0;
            Res1.setText("");
            Res2.setText("");
        }

    }

    private void minus() {
        int a=text.length();
        String s01="+";
        String s02="-";
        String s03="×";
        String s04="÷";
        String s05=".";
        if (a!=0) {
            String s=text.substring(a-1,a);
            String s0=text.substring(0,a-1);
            if (s01.equals(s) || s02.equals(s) || s03.equals(s) || s04.equals(s) || s05.equals(s)) {
                text = s0+"-";
            }else {
                text += "-";
            }
        } else {
            text = "";
            result =0.0;
            Res1.setText("");
            Res2.setText("");
        }

    }

    private void multiply() {

        int a=text.length();
        String s01="+";
        String s02="-";
        String s03="×";
        String s04="÷";
        String s05=".";
        if (a!=0) {
            String s=text.substring(a-1,a);
            String s0=text.substring(0,a-1);
            if (s01.equals(s) || s02.equals(s) || s03.equals(s) || s04.equals(s) || s05.equals(s)) {
                text = s0+"×";
            }else {
                text += "×";
            }
        } else {
            text = "";
            result =0.0;
            Res1.setText("");
            Res2.setText("");
        }

    }

    private void divide() {

        int a=text.length();
        String s01="+";
        String s02="-";
        String s03="×";
        String s04="÷";
        String s05=".";
        if (a!=0) {
            String s=text.substring(a-1,a);
            String s0=text.substring(0,a-1);
            if (s01.equals(s) || s02.equals(s) || s03.equals(s) || s04.equals(s) || s05.equals(s)) {
                text = s0+"÷";
            }else {
                text += "÷";
            }
        } else {
            text = "";
            result =0.0;
            Res1.setText("");
            Res2.setText("");
        }
    }

    //计算输出结果
    private void result() {
        result = testOperation(text);
    }


    public Double testOperation(String s){
        //分割字符然后放进数组
        String s1 =s.replace("+","-");//替换
        String[] str = s1.split("-");
        double total1=0;
        //先遍历数组,把里面的乘除结果算出来
        //增强型循环 str去遍历str1数组
        for(String str1:str){
            if(str1.contains("×")||str1.contains("÷")){
                double total = 0;
                for(int i =0;i<str1.length();){
                    int count =1;
                    for(int j =i+1;j<str1.length();j++){
                        char c =str1.charAt(j);//charat返回指定索引处的字符
                        if(c=='×'||c=='÷'){// 如果左边执行结果为true则不再执行 ,返回true
                            break ;
                        }else{
                            count++;
                        }
                    }


                    //将数字截取出来
                    String s2 =str1.substring(i,i+count);
                    double d = Double.parseDouble(s2);

                    if(i==0){ //i为0则显示框内没有乘除符号
                        total = d;
                    }else{
                        char c1 = str1.charAt(i-1); //返回数字的后一位 看符号是什么
                        if(c1=='×'){
                            total*=d;
                        }else if(c1=='÷'){
                            //如果除数为0,直接返回null;
                            if(d == 0)
                                return null;
                            total/=d;
                        }
                    }
                    i+=count+1;
                }
                s= s.replace(str1, total+"");
            }
        }
        //进行加减运算
        for(int i =0;i<s.length();i++){
            int count =1;
            for(int j=i+1;j<s.length();j++){
                char c = s.charAt(j);
                if(c=='+'||c=='-'){
                    break;
                }else{
                    count++;
                }
            }
            String s3= s.substring(i,i+count);
            double d2 = Double.parseDouble(s3);
            if(i==0){
                total1 = d2;
            }else{
                char c = s.charAt(i-1);
                if(c=='+'){
                    total1+=d2;
                }else if(c=='-'){
                    total1-=d2;
                }
            }
            i+=count;
        }
        return total1;
    }

}

;