【Android Studio】Simple Calculator

Simple calculator requirements:

1. Simple operation, easy to master, and simple interface.
2. Convenient for operations such as addition, subtraction, multiplication, and division. Numbers are kept to two decimal places.
3. Contains decimal point operations and input fallback functions.
4. Ability to perform multiple superposition operations.
5. The system can perform multiple superposition operations.
6. The system can run stably.

The function diagram is as follows:
Insert image description here
the logic flow diagram is as follows:
Insert image description here

Project establishment

Project Creation.
Create a new project: Set the project name to MainActivity.
Select the Blank Activity or empty Activity template in the Activity template.
The remaining operations remain unchanged and the project creation is completed.

Implementation of calculator main interface

1. Design interface layout XML file.
In the resllayout\ directory of the Android Studio project, find the activity_main.xml file, which is used as the settings file for the main interface layout. In the layout file, add an AbsoluteLayout (absolute layout manager) component to display the function icon and the text on the icon. In the AbsoluteLayout (absolute layout manager), define a TextView to display the intermediate numbers and results. Define 17 Buttons are used to input numbers from 0 to 9 and +, -, *, 1, =,. operation symbols and the "Back" input back button, and use the "android:onClick" attribute to bind their response functions, the specific code as follows.
The code is as follows:

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:id="@+id/txtResult"
        android:layout_width="460px"
        android:layout_height="80px"
        android:background="#ffffffff"
        android:text="0.0"
        android:textSize="28sp"
        android:textStyle="bold"
        android:textColor="#ff333333"
        android:layout_x="11dp"
        android:layout_y="16dp"
        android:gravity="end|right"
        android:padding="2px" />
    <Button android:id="@+id/btn1"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="1"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="15dp"
        android:layout_y="85dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btn2"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="2"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="85dp"
        android:layout_y="85dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btn3"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="3"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="155dp"
        android:layout_y="85dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btnAdd"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="+"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="225dp"
        android:layout_y="85dp"
        android:onClick="add"/>
    <Button android:id="@+id/btn4"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="4"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="15dp"
        android:layout_y="155dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btn5"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="5"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="85dp"
        android:layout_y="155dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btn6"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="6"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="155dp"
        android:layout_y="155dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btnsub"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="-"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="225dp"
        android:layout_y="155dp"
        android:onClick="sub"/>
    <Button android:id="@+id/btn7"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="7"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="15dp"
        android:layout_y="225dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btn8"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="8"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="85dp"
        android:layout_y="225dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btn9"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="9"
        android:textSize="20sp"
        android:background="#ff5599ff"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="155dp"
        android:layout_y="225dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btnMul"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="*"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="225dp"
        android:layout_y="225dp"
        android:onClick="mul"/>
    <Button android:id="@+id/btn0"
        android:layout_width="130px"
        android:layout_height="80px"
        android:text="0"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="15dp"
        android:layout_y="295dp"
        android:onClick="digital_click"/>
    <Button android:id="@+id/btnPoint"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="."
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="155dp"
        android:layout_y="295dp"
        android:onClick="point_click"/>
    <Button android:id="@+id/btnDiv"
        android:layout_width="80px"
        android:layout_height="80px"
        android:text="/"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="225dp"
        android:layout_y="295dp"
        android:onClick="div"/>
    <Button android:id="@+id/btndel"
        android:layout_width="80px"
        android:layout_height="85px"
        android:text="back"
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="#ffff0000"
        android:gravity="center"
        android:layout_x="15dp"
        android:layout_y="365dp"
        android:onClick="del"/>
    <Button android:id="@+id/btnequ"
        android:layout_width="290px"
        android:layout_height="80px"
        android:text="="
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_x="85dp"
        android:layout_y="365dp"
        android:onClick="equ"/>
</AbsoluteLayout>

Implementation of calculator logic

Since the program needs to use the module definition in the library function, when writing the program, the corresponding content needs to be "imported"
to support the use of this class in the program. For example, if you want to use the "Button" component in Android Studio, you must import the "Button" class. The file header of the Android calculator is as follows:

package com.example.mainacticity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

The overall code is as follows:

package com.example.mainacticity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    
    
    double firstNum=0;//第一个输入的数据
    char currentSign='+';//记录第一次输入的符号
    StringBuffer currentNum=new StringBuffer();//得到textview中的数据
    boolean isFirstPoint=false;//第一个数据是否是小数点
    TextView txtResult;//输出结果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtResult=(TextView) findViewById(R.id.txtResult);
    }

    //1.输入框恢复与初始化
    public void init(){
    
    
        currentNum.delete(0,currentNum.length());//设置当前textview中的值为0
        isFirstPoint=false;
    }

    //2.数制转换函数
    public double stringToDouble(){
    
    
        if(currentNum.length()==0){
    
    
            return 0;
        }
        double result=Double.parseDouble(currentNum.toString());
        return result;
    }

    //3.计算函数
    public double calcu(){
    
    
        double result=0;
        switch (currentSign){
    
    
            case '+':
                result=firstNum+stringToDouble();
                break;
            case '-':
                result=firstNum-stringToDouble();
                break;
            case '*':
                result=firstNum*stringToDouble();
                break;
            case '/':
                result=firstNum/stringToDouble();
                break;
            default:
                break;
        }
        //对小数点后的数据进行格式化
        NumberFormat format=NumberFormat.getInstance();
        format.setMaximumFractionDigits(2);
        result=Double.parseDouble(format.format(result));
        return result;
    }

    //4显示数据函数
    public void display(){
    
    
        txtResult.setText(currentNum.toString());
    }

    //5数字按钮单击响应函数
    public void digital_click(View view){
    
    
        Button btnDigital=(Button)view;
        char text= btnDigital.getText().charAt(0);
        currentNum.append(text);
        display();
    }

    //6.加法按钮单击
    public void add(View view){
    
    
        double result=calcu();
        txtResult.setText(String.valueOf(result));
        firstNum=result;
        currentSign='+';
        init();
    }
    //7.单击减法
    public void sub(View view) {
    
    
        double result = calcu();
        txtResult.setText(String.valueOf(result));
        firstNum = result;
        currentSign = '-';
        init();
    }
    //8.单击乘法
    public void mul(View view) {
    
    
        double result = calcu();
        txtResult.setText(String.valueOf(result));
        firstNum = result;
        currentSign = '*';
        init();
    }
    //9.单击除法
    public void div(View view) {
    
    
        double result = calcu();
        txtResult.setText(String.valueOf(result));
        firstNum = result;
        currentSign = '/';
        init();
    }
    //10.处理等于函数
    public void equ(View view){
    
    
        double result=calcu();
        txtResult.setText(String.valueOf(result));
        firstNum=result;
        currentSign='+';
        init();

    }
    //11.小数点按钮单击相应函数
    public void point_click(View view){
    
    
        if(isFirstPoint){
    
    
            return;//当第一个数据为小数点时,程序返回
        }
        if(currentNum.length()==0){
    
    
            return;//当没有输入的数据时返回
        }
        Button btnPoint=(Button) view;
        char text=btnPoint.getText().charAt(0);
        currentNum.append(text);
        isFirstPoint=true;
        display();
    }

    //12.删除按钮单击相应
    public void del(View view){
    
    
        if(currentNum.length()>=1){
    
    
            currentNum.delete(currentNum.length()-1,currentNum.length());
        }
        if(currentNum.length()==0){
    
    
            init();
            display();
        }
        txtResult.setText(currentNum);

    }
}

Select the appropriate device to run:
Insert image description here
click the green triangle to run. After a short loading, the calculator interface will appear:
Insert image description here
perform calculations:
Insert image description here
22+23=45
Insert image description here
can perform simple addition, subtraction, multiplication and division operations, and will be automatically rounded to retain two digits after the decimal point.
Finish!

Guess you like

Origin blog.csdn.net/gelly_/article/details/131737149