Android Studio Simple Calculator

Table of contents

The first step is to create a new project

The second step is to design the UI

The third step is to implement the calculation logic

Step 4. Test the application


With the popularity of mobile Internet, mobile applications have become an indispensable part of people's lives. The calculator is one of the most widely used applications, so learning how to develop a simple calculator application is a good start for learning Android Studio development.

Android Studio is an integrated development environment (IDE) developed by Google for creating Android applications. It can help developers quickly design, develop and test applications. Next, I will introduce how to use Android Studio to create a simple calculator application.

The first step is to create a new project

Open Android Studio and create a new project.

 Select the "Empty Activity" template, then give the project a name and choose a location to store the project.

The second step is to design the UI

We need to design a simple calculator interface. Add two EditText elements to display the calculator input and output results. Next, add multiple Button elements in the XML file in sequence. These elements will be arranged as various calculation operations that need to be used in the calculator. These Button elements use GridLayout to arrange them together.

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <EditText
            android:id="@+id/ed_srk"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:hint="input box"/>
        <EditText
            android:id="@+id/ed_sck"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:hint="Output box"/>
    </GridLayout>

   <GridLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center|top"
       android:orientation="horizontal"
       android:rowCount="5"
       android:columnCount="4">
       <Button
           android:id="@+id/btn_c"
           android:layout_width="160dp"
           android:layout_height="60dp"
           android:layout_columnSpan="2"
           android:text="c"/>
       <Button
           android:id="@+id/btn_del"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="DEL"/>
       <Button
           android:id="@+id/btn_chu"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="/"/>
       <Button
           android:id="@+id/btn7"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="7"/>
       <Button
           android:id="@+id/btn8"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="8"/>
       <Button
           android:id="@+id/btn9"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="9"/>
       <Button
           android:id="@+id/btn_che"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="*"/>
       <Button
           android:id="@+id/btn4"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="4"/>
       <Button
           android:id="@+id/btn5"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="5"/>
       <Button
           android:id="@+id/btn6"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="6"/>
       <Button
           android:id="@+id/btn_jih"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="-"/>
       <Button
           android:id="@+id/btn1"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="1"/>
       <Button
           android:id="@+id/btn2"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="2"/>
       <Button
           android:id="@+id/btn3"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="3"/>
       <Button
           android:id="@+id/btn_jah"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="+"/>
       <Button
           android:id="@+id/btn_yl"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="Reserved"/>
       <Button
           android:id="@+id/btn0"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="0"/>
       <Button
           android:id="@+id/btn_dian"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="."/>
       <Button
           android:id="@+id/btn_dy"
           android:layout_width="80dp"
           android:layout_height="60dp"
           android:text="="/>
   </GridLayout>
</GridLayout>

The third step is to implement the calculation logic

Define a class in a Java file, inherit from Activity, and implement the OnClickListener interface. The purpose of this step is to define separate event handlers for each Button element. In this class, it will be implemented to find all Button elements and set event handlers for them, receive user input, and calculate the results that should be displayed based on different calculation operations and display them in the output area.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //Create object
    private Button mBtn1, mBtn2, mBtn3, mBtn4, mBtn5,
            mBtn6, mBtn7, mBtn8, mBtn9, mBtn0,
            mBtnc, mBtndel, mBtnchu, mBtnche, mBtnjia,
            mBtnjian, mBtndian, mBtndy, mBtnyl;
    private EditText edsrk, edsck;

    //Determine whether the text box is empty
    private boolean deng_flag;

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

        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
        mBtn4 = findViewById(R.id.btn4);
        mBtn5 = findViewById(R.id.btn5);
        mBtn6 = findViewById(R.id.btn6);
        mBtn7 = findViewById(R.id.btn7);
        mBtn8 = findViewById(R.id.btn8);
        mBtn9 = findViewById(R.id.btn9);
        mBtn0 = findViewById(R.id.btn0);
        mBtnc = findViewById(R.id.btn_c);
        mBtndel = findViewById(R.id.btn_del);
        mBtnchu = findViewById(R.id.btn_chu);
        mBtnjia = findViewById(R.id.btn_jah);
        mBtnjian = findViewById(R.id.btn_jih);
        mBtndian = findViewById(R.id.btn_dian);
        mBtndy = findViewById(R.id.btn_dy);
        mBtnyl = findViewById(R.id.btn_yl);
        mBtnche = findViewById(R.id.btn_che);

        //Button set click event
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
        mBtn3.setOnClickListener(this);
        mBtn4.setOnClickListener(this);
        mBtn5.setOnClickListener(this);
        mBtn6.setOnClickListener(this);
        mBtn7.setOnClickListener(this);
        mBtn8.setOnClickListener(this);
        mBtn9.setOnClickListener(this);
        mBtn0.setOnClickListener(this);
        mBtnc.setOnClickListener(this);
        mBtndel.setOnClickListener(this);
        mBtnchu.setOnClickListener(this);
        mBtnjia.setOnClickListener(this);
        mBtnjian.setOnClickListener(this);
        mBtndian.setOnClickListener(this);
        mBtndy.setOnClickListener(this);
        mBtnyl.setOnClickListener(this);
        mBtnche.setOnClickListener(this);

        edsrk = findViewById(R.id.ed_srk);
        edsck = findViewById(R.id.ed_sck);
    }

    @Override
    public void onClick(View view) {
        String input = edsrk.getText().toString();
        String output = edsck.getText().toString();

        switch (view.getId()) {
            case R.id.btn1:
            case R.id.btn2:
            case R.id.btn3:
            case R.id.btn4:
            case R.id.btn5:
            case R.id.btn6:
            case R.id.btn7:
            case R.id.btn8:
            case R.id.btn9:
            case R.id.btn0:

            case R.id.btn_dian:
                if (deng_flag) {
                    your_flag = false;
                    edsrk.setText(null);
                    edsrk.setText(((Button) view).getText());
                } else {
                    edsrk.setText(input + ((Button) view).getText());
                }
                break;

            houses R.id.btn_che:
            case R.id.btn_jah:
            case R.id.btn_jih:
            case R.id.btn_is:
                edsrk.setText(input + " " + ((Button) view).getText() + " ");
                break;

            case R.id.btn_dy:
                //Call the method below to calculate the result
                getResult();
                break;
                
            case R.id.btn_yl:
            case R.id.btn_c:
                edsrk.setText(null);
                edsck.setText(null);
                break;
                
            case R.id.btn_part:
                //Determine whether it is empty and then delete it
                if (deng_flag) {
                    your_flag = false;
                    edsrk.setText("");
                } else if (input != null && !input.equals("")) {
                    edsrk.setText(input.substring(0, input.length() - 1));
                }
                break;
        }
    }

    private void getResult() {
        String input = edsrk.getText().toString();

        deng_flag = true;

        double dResult = 0;
        int iResult = 0;

        //Intercept the string before the operator
        String s1 = input.substring(0, input.indexOf(" "));
        //interception operator
        String op = input.substring(input.indexOf(" ") + 1, input.indexOf(" ") + 2);
        //Intercept the string after the operator
        String s2 = input.substring(input.indexOf(" ") + 3);

        //Convert string to value based on s1, s2
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);

        //Get the calculation result based on the input operation symbol and return the result to the output box
        if (op.equals("+")) {
            dResult = d1 + d2;
        } else if (op.equals("-")) {
            dResult = d1 - d2;
        } else if (op.equals("*")) {
            dResult = d1 * d2;
        } else if (op.equals("/")) {
            if (d1 == 0) {
                dResult = 0;
            } else {
                dResult = d1 / d2;
            }
        } else {
            edsck.setText(dResult + "");
        }

        //If the value in the input box contains a decimal point or the operator is a division sign, the result returns a double value.
        if (s1.contains(".") || s2.contains(".") || op.equals("/")) {
            edsck.setText(dResult + "");
        } else {
            iResult = (int) dResult;
            edsck.setText(iResult + "");
        }
    }
}

Step 4. Test the application

(realize the effect)

The above are the steps to create a simple calculator application using Android Studio.

This article takes a simple calculator as an example to briefly introduce the basic process of application development using Android Studio, including steps such as creating a new project, adding user interface elements, defining event handlers, testing the application, and publishing the application. Just follow these basic steps and you can build a simple yet full-featured calculator app in just a matter of seconds.

Guess you like

Origin blog.csdn.net/weixin_68155905/article/details/131310870