最新android studio制作简易计算器代码
时间: 2023-09-09 14:13:02 浏览: 131
Android studio设计简易计算器
好的,以下是最新android studio制作简易计算器的代码:
```
package com.example.calculator;
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 input1, input2;
Button add, subtract, multiply, divide;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input1 = findViewById(R.id.input1);
input2 = findViewById(R.id.input2);
add = findViewById(R.id.add);
subtract = findViewById(R.id.subtract);
multiply = findViewById(R.id.multiply);
divide = findViewById(R.id.divide);
result = findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(input1.getText().toString());
int num2 = Integer.parseInt(input2.getText().toString());
int sum = num1 + num2;
result.setText("Result: " + sum);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(input1.getText().toString());
int num2 = Integer.parseInt(input2.getText().toString());
int difference = num1 - num2;
result.setText("Result: " + difference);
}
});
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(input1.getText().toString());
int num2 = Integer.parseInt(input2.getText().toString());
int product = num1 * num2;
result.setText("Result: " + product);
}
});
divide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(input1.getText().toString());
int num2 = Integer.parseInt(input2.getText().toString());
int quotient = num1 / num2;
result.setText("Result: " + quotient);
}
});
}
}
```
希望能对您有所帮助!现在您有什么需要我帮忙回答的问题吗?
阅读全文