用GridLayout布局管理器设计一个界面,如图所示,共7个按钮,3个文本框。当向第一个和第二个文本框中输入数字,并点击运算符按钮时,则第一行第1个按钮文本变为相应的运算符;当点击OK时,将计算的结果添加到第一行最后一个文本框中。 要求:用JAVA代码实现
时间: 2024-03-12 15:48:57 浏览: 149
好的,我会尽力回答您的问题。
首先,您需要在您的项目中添加GridLayout布局管理器。在您的Activity或Fragment中,您可以这样写:
```java
GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(4);
gridLayout.setRowCount(4);
```
这将创建一个4列4行的GridLayout。接下来,您需要创建按钮和文本框,并将它们添加到GridLayout中。您可以使用如下代码创建一个按钮:
```java
Button button = new Button(this);
button.setText("Button");
```
您可以使用如下代码创建一个文本框:
```java
EditText editText = new EditText(this);
```
然后,您需要将它们添加到GridLayout中。您可以使用如下代码将一个视图添加到GridLayout中:
```java
gridLayout.addView(view, layoutParams);
```
其中,`view`是您要添加到GridLayout中的视图,`layoutParams`是一个GridLayout.LayoutParams对象,指定视图应该放置在GridLayout中的哪个位置。
最后,您需要添加一些逻辑来处理输入和计算结果。您可以使用如下代码来监听按钮的点击事件:
```java
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 处理点击事件
}
});
```
在处理点击事件时,您可以获取文本框中的输入,进行计算,并将结果显示在结果文本框中。
综上所述,以下是一个用GridLayout布局管理器设计的界面的示例代码:
```java
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private EditText resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建GridLayout
GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(4);
gridLayout.setRowCount(4);
// 创建按钮和文本框
Button addButton = new Button(this);
addButton.setText("+");
Button subtractButton = new Button(this);
subtractButton.setText("-");
Button multiplyButton = new Button(this);
multiplyButton.setText("*");
Button divideButton = new Button(this);
divideButton.setText("/");
Button clearButton = new Button(this);
clearButton.setText("C");
Button okButton = new Button(this);
okButton.setText("OK");
editText1 = new EditText(this);
editText2 = new EditText(this);
resultText = new EditText(this);
resultText.setEnabled(false);
// 添加视图到GridLayout中
gridLayout.addView(editText1, new GridLayout.LayoutParams(GridLayout.spec(0, 1), GridLayout.spec(0, 3)));
gridLayout.addView(editText2, new GridLayout.LayoutParams(GridLayout.spec(1, 1), GridLayout.spec(0, 3)));
gridLayout.addView(addButton, new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(0)));
gridLayout.addView(subtractButton, new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(1)));
gridLayout.addView(multiplyButton, new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(2)));
gridLayout.addView(divideButton, new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(3)));
gridLayout.addView(clearButton, new GridLayout.LayoutParams(GridLayout.spec(3, 1), GridLayout.spec(0)));
gridLayout.addView(okButton, new GridLayout.LayoutParams(GridLayout.spec(3, 1), GridLayout.spec(1)));
gridLayout.addView(resultText, new GridLayout.LayoutParams(GridLayout.spec(3, 1), GridLayout.spec(2, 2)));
// 添加点击事件处理逻辑
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation("+");
}
});
subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation("-");
}
});
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation("*");
}
});
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
performCalculation("/");
}
});
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText1.setText("");
editText2.setText("");
resultText.setText("");
}
});
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text1 = editText1.getText().toString();
String text2 = editText2.getText().toString();
if (TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2)) {
Toast.makeText(MainActivity.this, "请输入数字", Toast.LENGTH_SHORT).show();
} else {
double num1 = Double.parseDouble(text1);
double num2 = Double.parseDouble(text2);
double result = performCalculation("+", num1, num2);
resultText.setText(String.valueOf(result));
}
}
});
// 设置ContentView为GridLayout
setContentView(gridLayout);
}
private double performCalculation(String operator, double num1, double num2) {
switch (operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
if (num2 == 0) {
Toast.makeText(this, "除数不能为0", Toast.LENGTH_SHORT).show();
return 0;
} else {
return num1 / num2;
}
default:
return 0;
}
}
private void performCalculation(String operator) {
String text1 = editText1.getText().toString();
String text2 = editText2.getText().toString();
if (TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2)) {
Toast.makeText(this, "请输入数字", Toast.LENGTH_SHORT).show();
} else {
double num1 = Double.parseDouble(text1);
double num2 = Double.parseDouble(text2);
double result = performCalculation(operator, num1, num2);
Button operatorButton = getOperatorButton();
operatorButton.setText(operator);
resultText.setText(String.valueOf(result));
}
}
private Button getOperatorButton() {
GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);
return (Button) gridLayout.getChildAt(2);
}
}
```
希望这能帮助到您!
阅读全文