android studio简易计算器的代码实现,功能包括加,减,乘,除,平方根,倒数
时间: 2023-05-29 20:02:51 浏览: 711
下面是Android Studio简易计算器的代码实现:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button0, button1, button2, button3, button4, button5,
button6, button7, button8, button9, buttonAdd, buttonMinus,
buttonDivide, buttonMultiply, buttonClear, buttonEqual, buttonSquareRoot, buttonInverse, buttonDot;
private double num1 = 0, num2 = 0;
private String operator = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
button0 = findViewById(R.id.button0);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
button5 = findViewById(R.id.button5);
button6 = findViewById(R.id.button6);
button7 = findViewById(R.id.button7);
button8 = findViewById(R.id.button8);
button9 = findViewById(R.id.button9);
buttonAdd = findViewById(R.id.buttonAdd);
buttonMinus = findViewById(R.id.buttonMinus);
buttonDivide = findViewById(R.id.buttonDivide);
buttonMultiply = findViewById(R.id.buttonMultiply);
buttonClear = findViewById(R.id.buttonClear);
buttonEqual = findViewById(R.id.buttonEqual);
buttonSquareRoot = findViewById(R.id.buttonSquareRoot);
buttonInverse = findViewById(R.id.buttonInverse);
buttonDot = findViewById(R.id.buttonDot);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "9");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operator = "+";
num1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
});
buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operator = "-";
num1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
});
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operator = "/";
num1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
});
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
operator = "*";
num1 = Double.parseDouble(editText.getText().toString());
editText.setText("");
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num2 = Double.parseDouble(editText.getText().toString());
if (operator.equals("+")) {
double result = num1 + num2;
editText.setText(String.valueOf(result));
} else if (operator.equals("-")) {
double result = num1 - num2;
editText.setText(String.valueOf(result));
} else if (operator.equals("/")) {
double result = num1 / num2;
editText.setText(String.valueOf(result));
} else if (operator.equals("*")) {
double result = num1 * num2;
editText.setText(String.valueOf(result));
}
}
});
buttonSquareRoot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Double.parseDouble(editText.getText().toString());
double result = Math.sqrt(num1);
editText.setText(String.valueOf(result));
}
});
buttonInverse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Double.parseDouble(editText.getText().toString());
double result = 1 / num1;
editText.setText(String.valueOf(result));
}
});
buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + ".");
}
});
}
}
```
在这个代码中,我们设置了一个`operator`变量来存储操作符,并在每次点击加减乘除按钮时将第一个数字存储在`num1`变量中,清空文本框中的文本。然后,当点击等号按钮时,我们将第二个数字存储在`num2`变量中,执行所需的操作并将答案显示在文本框中。
对于“平方根”和“倒数”按钮,我们只需从文本框中获取第一个数字,并执行所需的操作即可。
阅读全文