📌  相关文章
📜  如何使用 Android Studio 构建一个简单的计算器应用程序?

📅  最后修改于: 2022-05-13 01:55:43.275000             🧑  作者: Mango

如何使用 Android Studio 构建一个简单的计算器应用程序?

先决条件:

  • 面向初学者的 Android 应用开发基础知识
  • 安装和设置 Android Studio 指南
  • 安卓 |从第一个 app/android 项目开始
  • 安卓 |运行您的第一个 Android 应用程序

创建一个简单的计算器,它可以根据用户输入执行基本的算术运算,例如加法、减法、乘法或除法。下面给出了一个示例视频,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

第 2 步:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件。下面是activity_main.xml文件的代码。

XML


 
    
    
 
    
    
 
    
    
 
    
    
 
    
    


Java
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    EditText e1, e2;
    TextView t1;
    int num1, num2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    // a public method to get the input numbers
    public boolean getNumbers() {
 
        // defining the edit text 1 to e1
        e1 = (EditText) findViewById(R.id.num1);
 
        // defining the edit text 2 to e2
        e2 = (EditText) findViewById(R.id.num2);
 
        // defining the text view to t1
        t1 = (TextView) findViewById(R.id.result);
 
        // taking input from text box 1
        String s1 = e1.getText().toString();
 
        // taking input from text box 2
        String s2 = e2.getText().toString();
 
        // condition to check if box is not empty
        if ((s1.equals(null) && s2.equals(null))
                || (s1.equals("") && s2.equals(""))) {
 
            String result = "Please enter a value";
            t1.setText(result);
 
            return false;
        } else {
            // converting string to int.
            num1 = Integer.parseInt(s1);
 
            // converting string to int.
            num2 = Integer.parseInt(s2);
        }
 
        return true;
    }
 
    // a public method to perform addition
    public void doSum(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            int sum = num1 + num2;
            t1.setText(Integer.toString(sum));
        }
    }
 
    // a public method to perform power function
    public void doPow(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            double sum = Math.pow(num1, num2);
            t1.setText(Double.toString(sum));
        }
    }
 
    // a public method to perform subtraction
    public void doSub(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            int sum = num1 - num2;
            t1.setText(Integer.toString(sum));
        }
    }
 
    // a public method to perform multiplication
    public void doMul(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            int sum = num1 * num2;
            t1.setText(Integer.toString(sum));
        }
    }
 
    // a public method to perform Division
    public void doDiv(View v) {
 
        // get the input numbers
        if (getNumbers()) {
 
            // displaying the text in text view assigned as t1
            double sum = num1 / (num2 * 1.0);
            t1.setText(Double.toString(sum));
        }
    }
 
    // a public method to perform modulus function
    public void doMod(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            double sum = num1 % num2;
            t1.setText(Double.toString(sum));
        }
    }
}


使用此代码后,用户界面将如下所示:

第 3 步:使用 主要活动。Java

打开主活动。在类中的Java文件中,创建一个名为 doSum(View v) 的方法。在这种方法中,首先,我们必须将两个 EditText 与变量链接起来,以便我们可以将它们用于我们的输入。因此,将那些编辑框与我们编写的变量链接起来

"EditText e1=(EditText )findViewById(R.id.num1);"

这里 num1 是文本框的 id,我们只是给 id 为“num1”的文本框提供了一个变量名“e1”。同样,我们必须对变量名为“e2”的第二个文本框使用相同的语句。对于第三个文本框,我们使用了

"TextView t1=(TextView) findViewById(R.id.result);"

这里我们使用了 TextView,因为我们只需要显示文本,避免它是用户可更改的。现在我们必须使用getText()函数以字符串的形式输入数字。输入语句将是

"String s11=e1.getText().toString();"

这里 s11 存储在文本框 1 中输入的数字。我们必须对另一个文本框(e2)做同样的事情。现在以 int 形式存储数字并应用加法。将附加值存储在另一个变量中。要显示存储在 sum 我们必须使用 setText() 如下:

result.setText(final_sum.toString())

final_sum 存储总和,有必要将其转换为字符串(.toString())。下面是MainActivity 的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    EditText e1, e2;
    TextView t1;
    int num1, num2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    // a public method to get the input numbers
    public boolean getNumbers() {
 
        // defining the edit text 1 to e1
        e1 = (EditText) findViewById(R.id.num1);
 
        // defining the edit text 2 to e2
        e2 = (EditText) findViewById(R.id.num2);
 
        // defining the text view to t1
        t1 = (TextView) findViewById(R.id.result);
 
        // taking input from text box 1
        String s1 = e1.getText().toString();
 
        // taking input from text box 2
        String s2 = e2.getText().toString();
 
        // condition to check if box is not empty
        if ((s1.equals(null) && s2.equals(null))
                || (s1.equals("") && s2.equals(""))) {
 
            String result = "Please enter a value";
            t1.setText(result);
 
            return false;
        } else {
            // converting string to int.
            num1 = Integer.parseInt(s1);
 
            // converting string to int.
            num2 = Integer.parseInt(s2);
        }
 
        return true;
    }
 
    // a public method to perform addition
    public void doSum(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            int sum = num1 + num2;
            t1.setText(Integer.toString(sum));
        }
    }
 
    // a public method to perform power function
    public void doPow(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            double sum = Math.pow(num1, num2);
            t1.setText(Double.toString(sum));
        }
    }
 
    // a public method to perform subtraction
    public void doSub(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            int sum = num1 - num2;
            t1.setText(Integer.toString(sum));
        }
    }
 
    // a public method to perform multiplication
    public void doMul(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            int sum = num1 * num2;
            t1.setText(Integer.toString(sum));
        }
    }
 
    // a public method to perform Division
    public void doDiv(View v) {
 
        // get the input numbers
        if (getNumbers()) {
 
            // displaying the text in text view assigned as t1
            double sum = num1 / (num2 * 1.0);
            t1.setText(Double.toString(sum));
        }
    }
 
    // a public method to perform modulus function
    public void doMod(View v) {
 
        // get the input numbers
        if (getNumbers()) {
            double sum = num1 % num2;
            t1.setText(Double.toString(sum));
        }
    }
}

输出: