用android studio制作一个能加减乘除的计算器附上代码
时间: 2023-11-02 11:06:45 浏览: 109
好的,以下是一个简单的能够进行加减乘除运算的计算器的代码。你可以在 Android Studio 中创建一个新的项目,将以下代码复制到 MainActivity.java 文件中,然后运行即可。
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText firstNumberEditText, secondNumberEditText;
TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumberEditText = findViewById(R.id.firstNumberEditText);
secondNumberEditText = findViewById(R.id.secondNumberEditText);
resultTextView = findViewById(R.id.resultTextView);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double firstNumber = Double.parseDouble(firstNumberEditText.getText().toString());
double secondNumber = Double.parseDouble(secondNumberEditText.getText().toString());
double result = firstNumber + secondNumber;
resultTextView.setText(Double.toString(result));
}
});
Button subtractButton = findViewById(R.id.subtractButton);
subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double firstNumber = Double.parseDouble(firstNumberEditText.getText().toString());
double secondNumber = Double.parseDouble(secondNumberEditText.getText().toString());
double result = firstNumber - secondNumber;
resultTextView.setText(Double.toString(result));
}
});
Button multiplyButton = findViewById(R.id.multiplyButton);
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double firstNumber = Double.parseDouble(firstNumberEditText.getText().toString());
double secondNumber = Double.parseDouble(secondNumberEditText.getText().toString());
double result = firstNumber * secondNumber;
resultTextView.setText(Double.toString(result));
}
});
Button divideButton = findViewById(R.id.divideButton);
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double firstNumber = Double.parseDouble(firstNumberEditText.getText().toString());
double secondNumber = Double.parseDouble(secondNumberEditText.getText().toString());
double result = firstNumber / secondNumber;
resultTextView.setText(Double.toString(result));
}
});
}
}
```
在布局文件 activity_main.xml 中,可以添加以下代码来创建用户界面:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<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="match_parent"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/subtractButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract"/>
<Button
android:id="@+id/multiplyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply"/>
<Button
android:id="@+id/divideButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide"/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Result will appear here"
android:textSize="24sp"/>
</LinearLayout>
```
运行代码后,你就可以在应用程序中输入两个数字并选择所需的运算类型,然后计算器将返回结果。
阅读全文