用Android studio实现计算机
时间: 2024-03-15 17:44:10 浏览: 117
要在Android Studio上实现计算器,您需要进行以下步骤:
1. 创建一个新的Android项目,并在XML布局文件中添加一个EditText和多个Button,用于显示和输入数字和计算操作符。
2. 在活动(Activity)中,获取所有Button的引用,并为它们添加单击监听器(setOnClickListener),以便在用户单击按钮时执行相应的操作。
3. 实现逻辑以响应用户的输入。例如,您可以使用字符串来保存用户输入的数字和操作符,并使用Java的数学库(Math类)来执行计算。
4. 最后,将计算结果显示在EditText中。
下面是一个简单的示例代码,用于实现一个基本的计算器:
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:inputType="numberDecimal"
android:gravity="right" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="4"
android:columnCount="4">
<Button
android:id="@+id/button1"
android:text="1"
android:layout_row="2"
android:layout_column="0" />
<Button
android:id="@+id/button2"
android:text="2"
android:layout_row="2"
android:layout_column="1" />
<Button
android:id="@+id/button3"
android:text="3"
android:layout_row="2"
android:layout_column="2" />
<Button
android:id="@+id/addButton"
android:text="+"
android:layout_row="2"
android:layout_column="3" />
<Button
android:id="@+id/button4"
android:text="4"
android:layout_row="1"
android:layout_column="0" />
<Button
android:id="@+id/button5"
android:text="5"
android:layout_row="1"
android:layout_column="1" />
<Button
android:id="@+id/button6"
android:text="6"
android:layout_row="1"
android:layout_column="2" />
<Button
android:id="@+id/subtractButton"
android:text="-"
android:layout_row="1"
android:layout_column="3" />
<Button
android:id="@+id/button7"
android:text="7"
android:layout_row="0"
android:layout_column="0" />
<Button
android:id="@+id/button8"
android:text="8"
android:layout_row="0"
android:layout_column="1" />
<Button
android:id="@+id/button9"
android:text="9"
android:layout_row="0"
android:layout_column="2" />
<Button
android:id="@+id/multiplyButton"
android:text="*"
android:layout_row="0"
android:layout_column="3" />
<Button
android:id="@+id/decimalButton"
android:text="."
android:layout_row="3"
android:layout_column="0" />
<Button
android:id="@+id/button0"
android:text="0"
android:layout_row="3"
android:layout_column="1" />
<Button
android:id="@+id/equalsButton"
android:text="="
android:layout_row="3"
android:layout_column="2" />
<Button
android:id="@+id/divideButton"
android:text="/"
android:layout_row="3"
android:layout_column="3" />
</GridLayout>
</LinearLayout>
```
Java代码:
```
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
private Button addButton, subtractButton, multiplyButton, divideButton, decimalButton, equalsButton;
private String currentNumber = "";
private String operator = "";
private double result = Double.NaN;
@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);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
decimalButton = findViewById(R.id.decimalButton);
equalsButton = findViewById(R.id.equalsButton);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateCurrentNumber("9");
}
});
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performOperation("+");
}
});
subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performOperation("-");
}
});
multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performOperation("*");
}
});
divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
performOperation("/");
}
});
decimalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!currentNumber.contains(".")) {
currentNumber += ".";
editText.setText(currentNumber);
}
}
});
equalsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculateResult();
}
});
}
private void updateCurrentNumber(String digit) {
currentNumber += digit;
editText.setText(currentNumber);
}
private void performOperation(String newOperator) {
if (!currentNumber.equals("")) {
if (!Double.isNaN(result)) {
calculateResult();
}
result = Double.parseDouble(currentNumber);
currentNumber = "";
}
operator = newOperator;
}
private void calculateResult() {
if (!Double.isNaN(result)) {
double secondNumber = Double.parseDouble(currentNumber);
switch (operator) {
case "+":
result += secondNumber;
break;
case "-":
result -= secondNumber;
break;
case "*":
result *= secondNumber;
break;
case "/":
result /= secondNumber;
break;
}
currentNumber = "";
editText.setText(String.valueOf(result));
result = Double.NaN;
}
}
}
```
这只是一个基本的示例,您可以根据自己的需求进行扩展和改进。
阅读全文