📜  Android |应用程序将两个数字相加(1)

📅  最后修改于: 2023-12-03 15:13:21.204000             🧑  作者: Mango

Android | 应用程序将两个数字相加

在本文中,将向您展示如何在Android应用程序中创建简单的计算应用程序,以将两个数字相加。我们将学习如何设计用户界面,如何获取用户输入,如何将它们添加在一起并将结果返回给用户。

设计用户界面

我们将使用Android Studio创建此应用程序。我们的用户界面将由两个EditText组成,用于输入两个数字,和一个Button,用于执行计算并显示结果。

以下是我们的用户界面的截图:

User Interface

我们将在activity_main.xml文件中定义此用户界面。以下是代码片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
    
         <EditText
              android:id="@+id/firstNumberEditText"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="Enter first number" />
    
         <EditText
              android:id="@+id/secondNumberEditText"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="Enter second number" />
    
         <Button
              android:id="@+id/addButton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Add" />
    
         <TextView
              android:id="@+id/resultTextView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:textColor="#000000"
              android:textSize="24sp" />
    
    </LinearLayout>

请注意,我们为EditText添加了hint,以指示用户应该输入什么。

获取用户输入

我们使用EditText控件从用户获取输入。为了将其添加在一起,我们需要像下面这样获取这些输入:

EditText firstNumberEditText = findViewById(R.id.firstNumberEditText);
EditText secondNumberEditText = findViewById(R.id.secondNumberEditText);
int firstNumber = Integer.parseInt(firstNumberEditText.getText().toString());
int secondNumber = Integer.parseInt(secondNumberEditText.getText().toString());

我们获取EditText的实例并通过调用getText()和toString()方法获取用户输入。由于EditText返回的是CharSequence,我们需要将其转换为int。

请注意,此代码应写在Button的OnClickListener中。

计算并将结果返回给用户

我们将在Button的OnClickListener中执行代码,将输入添加在一起并将结果设置为TextView。

以下是我们的代码片段:

Button addButton = findViewById(R.id.addButton);
TextView resultTextView = findViewById(R.id.resultTextView);

addButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        EditText firstNumberEditText = findViewById(R.id.firstNumberEditText);
        EditText secondNumberEditText = findViewById(R.id.secondNumberEditText);
        int firstNumber = Integer.parseInt(firstNumberEditText.getText().toString());
        int secondNumber = Integer.parseInt(secondNumberEditText.getText().toString());

        int result = firstNumber + secondNumber;

        resultTextView.setText(String.valueOf(result));
    }
});

请注意,我们将结果转换为字符串并将其设置为TextView的文本,以将其返回给用户。

完整代码

以下是我们的完整代码:

public class MainActivity extends AppCompatActivity {

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

        Button addButton = findViewById(R.id.addButton);
        TextView resultTextView = findViewById(R.id.resultTextView);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText firstNumberEditText = findViewById(R.id.firstNumberEditText);
                EditText secondNumberEditText = findViewById(R.id.secondNumberEditText);
                int firstNumber = Integer.parseInt(firstNumberEditText.getText().toString());
                int secondNumber = Integer.parseInt(secondNumberEditText.getText().toString());

                int result = firstNumber + secondNumber;

                resultTextView.setText(String.valueOf(result));
            }
        });
    }
}
结论

在本文中,我们学习了如何在Android应用程序中创建简单的计算应用程序,以将两个数字相加。我们学习了如何设计用户界面,如何获取用户输入以及如何将其添加在一起并将结果返回给用户。希望这篇文章对您有所帮助!