Android studio实现科学计算器代码
时间: 2023-09-23 18:03:28 浏览: 223
基于Android的科学计算器设计.zip
5星 · 资源好评率100%
以下是一个简单的科学计算器的 Android Studio 代码实现示例:
1. 首先,在 XML 布局文件中创建一个包含所有按钮的界面。
```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/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:inputType="text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_sin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sin" />
<Button
android:id="@+id/button_cos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cos" />
<Button
android:id="@+id/button_tan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tan" />
<Button
android:id="@+id/button_pow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="^" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="@+id/button_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:id="@+id/button_9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
<Button
android:id="@+id/button_divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="@+id/button_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="@+id/button_multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="@+id/button_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />
<Button
android:id="@+id/button_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="." />
<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<Button
android:id="@+id/button_equal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="=" />
</LinearLayout>
```
2. 在 Activity 中实现所有按钮的点击事件并处理计算逻辑。
```java
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 double value1 = Double.NaN;
private double value2;
private static final String ADDITION = "+";
private static final String SUBTRACTION = "-";
private static final String MULTIPLICATION = "*";
private static final String DIVISION = "/";
private String CURRENT_ACTION;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
Button button0 = findViewById(R.id.button_0);
Button button1 = findViewById(R.id.button_1);
Button button2 = findViewById(R.id.button_2);
Button button3 = findViewById(R.id.button_3);
Button button4 = findViewById(R.id.button_4);
Button button5 = findViewById(R.id.button_5);
Button button6 = findViewById(R.id.button_6);
Button button7 = findViewById(R.id.button_7);
Button button8 = findViewById(R.id.button_8);
Button button9 = findViewById(R.id.button_9);
Button buttonDot = findViewById(R.id.button_dot);
Button buttonClear = findViewById(R.id.button_clear);
Button buttonPlus = findViewById(R.id.button_plus);
Button buttonMinus = findViewById(R.id.button_minus);
Button buttonMultiply = findViewById(R.id.button_multiply);
Button buttonDivide = findViewById(R.id.button_divide);
Button buttonEqual = findViewById(R.id.button_equal);
Button buttonSin = findViewById(R.id.button_sin);
Button buttonCos = findViewById(R.id.button_cos);
Button buttonTan = findViewById(R.id.button_tan);
Button buttonPow = findViewById(R.id.button_pow);
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");
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + "0");
}
});
buttonDot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText(editText.getText() + ".");
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
value1 = Double.NaN;
value2 = Double.NaN;
editText.setText("");
}
});
buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = ADDITION;
editText.setText(String.valueOf(value1) + "+");
}
});
buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
editText.setText(String.valueOf(value1) + "-");
}
});
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
editText.setText(String.valueOf(value1) + "*");
}
});
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = DIVISION;
editText.setText(String.valueOf(value1) + "/");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
editText.setText(String.valueOf(value1));
value1 = Double.NaN;
}
});
buttonSin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double value = Double.parseDouble(editText.getText().toString());
double result = Math.sin(Math.toRadians(value));
editText.setText(String.valueOf(result));
}
});
buttonCos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double value = Double.parseDouble(editText.getText().toString());
double result = Math.cos(Math.toRadians(value));
editText.setText(String.valueOf(result));
}
});
buttonTan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double value = Double.parseDouble(editText.getText().toString());
double result = Math.tan(Math.toRadians(value));
editText.setText(String.valueOf(result));
}
});
buttonPow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
computeCalculation();
CURRENT_ACTION = "pow";
editText.setText(String.valueOf(value1) + "^");
}
});
}
private void computeCalculation() {
if (!Double.isNaN(value1)) {
value2 = Double.parseDouble(editText.getText().toString());
if (CURRENT_ACTION == ADDITION)
value1 = this.value1 + value2;
else if (CURRENT_ACTION == SUBTRACTION)
value1 = this.value1 - value2;
else if (CURRENT_ACTION == MULTIPLICATION)
value1 = this.value1 * value2;
else if (CURRENT_ACTION == DIVISION)
value1 = this.value1 / value2;
else if (CURRENT_ACTION == "pow")
value1 = Math.pow(value1, value2);
} else {
try {
value1 = Double.parseDouble(editText.getText().toString());
} catch (Exception e) {
}
}
}
}
```
这样,你就可以运行该应用程序并使用该科学计算器进行计算了。
阅读全文