用Android studio 写简单基础运算页面设计及代码
时间: 2023-05-21 14:06:42 浏览: 110
可以的,我可以回答这个问题。下面是一个简单的 Android Studio 基础运算页面设计及代码的例子:
1. 在布局文件 activity_main.xml 中添加以下代码:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入第一个数字"/>
<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入第二个数字"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加法运算"/>
<Button
android:id="@+id/subtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="减法运算"/>
<Button
android:id="@+id/multiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="乘法运算"/>
<Button
android:id="@+id/divide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="除法运算"/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算结果"/>
</LinearLayout>
```
2. 在 MainActivity.java 文件中添加以下代码:
```
public class MainActivity extends AppCompatActivity {
private EditText mNumber1;
private EditText mNumber2;
private Button mAdd;
private Button mSubtract;
private Button mMultiply;
private Button mDivide;
private TextView mResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNumber1 = findViewById(R.id.number1);
mNumber2 = findViewById(R.id.number2);
mAdd = findViewById(R.id.add);
mSubtract = findViewById(R.id.subtract);
mMultiply = findViewById(R.id.multiply);
mDivide = findViewById(R.id.divide);
mResult = findViewById(R.id.result);
mAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1 = Integer.parseInt(mNumber1.getText().toString());
int num2 = Integer.parseInt(mNumber2.getText().toString());
int result = num1 + num2;
mResult.setText(String.valueOf(result));
}
});
mSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1 = Integer.parseInt(mNumber1.getText().toString());
int num2 = Integer.parseInt(mNumber2.getText().toString());
int result = num1 - num2;
mResult.setText(String.valueOf(result));
}
});
mMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1 = Integer.parseInt(mNumber1.getText().toString());
int num2 = Integer.parseInt(mNumber2.getText().toString());
int result = num1 * num2;
mResult.setText(String.valueOf(result));
}
});
mDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num1 = Integer.parseInt(mNumber1.getText().toString());
int num2 = Integer.parseInt(mNumber2.getText().toString());
if (num2 == 0) {
mResult.setText("除数不能为零!");
} else {
float result = (float) num1 / num2;
mResult.setText(String.valueOf(result));
}
}
});
}
}
```
这样,我们就完成了一个简单的基础运算页面设计及代码的实现。
阅读全文