android studio计算器的代码
时间: 2023-09-20 18:09:27 浏览: 105
以下是一个简单的 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.TextView;
public class MainActivity extends AppCompatActivity {
TextView resultTextView;
int operand1;
int operand2;
String operator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.resultTextView);
}
public void numberClick(View view) {
Button button = (Button) view;
resultTextView.append(button.getText().toString());
}
public void operatorClick(View view) {
Button button = (Button) view;
operator = button.getText().toString();
operand1 = Integer.parseInt(resultTextView.getText().toString());
resultTextView.setText("");
}
public void equalsClick(View view) {
operand2 = Integer.parseInt(resultTextView.getText().toString());
int result = 0;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
resultTextView.setText(Integer.toString(result));
}
public void clearClick(View view) {
resultTextView.setText("");
operand1 = 0;
operand2 = 0;
}
}
```
activity_main.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/resultTextView"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="1"
android:layout_column="0"
android:layout_row="0"
android:onClick="numberClick"/>
<Button
android:text="2"
android:layout_column="1"
android:layout_row="0"
android:onClick="numberClick"/>
<Button
android:text="3"
android:layout_column="2"
android:layout_row="0"
android:onClick="numberClick"/>
<Button
android:text="+"
android:layout_column="3"
android:layout_row="0"
android:onClick="operatorClick"/>
<Button
android:text="4"
android:layout_column="0"
android:layout_row="1"
android:onClick="numberClick"/>
<Button
android:text="5"
android:layout_column="1"
android:layout_row="1"
android:onClick="numberClick"/>
<Button
android:text="6"
android:layout_column="2"
android:layout_row="1"
android:onClick="numberClick"/>
<Button
android:text="-"
android:layout_column="3"
android:layout_row="1"
android:onClick="operatorClick"/>
<Button
android:text="7"
android:layout_column="0"
android:layout_row="2"
android:onClick="numberClick"/>
<Button
android:text="8"
android:layout_column="1"
android:layout_row="2"
android:onClick="numberClick"/>
<Button
android:text="9"
android:layout_column="2"
android:layout_row="2"
android:onClick="numberClick"/>
<Button
android:text="*"
android:layout_column="3"
android:layout_row="2"
android:onClick="operatorClick"/>
<Button
android:text="0"
android:layout_column="1"
android:layout_row="3"
android:onClick="numberClick"/>
<Button
android:text="C"
android:layout_column="0"
android:layout_row="3"
android:onClick="clearClick"/>
<Button
android:text="/"
android:layout_column="3"
android:layout_row="3"
android:onClick="operatorClick"/>
<Button
android:text="="
android:layout_column="2"
android:layout_row="3"
android:onClick="equalsClick"/>
</GridLayout>
</LinearLayout>
```
这些代码将创建一个简单的四则运算计算器应用程序,具有数字按钮,运算符按钮和清除按钮。在用户输入操作数和运算符后,单击等号按钮将计算结果并将其显示在屏幕上。
阅读全文