Android Studio 制作简易计算器

 activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editTextNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入数字"
        android:inputType="numberDecimal"
        android:textSize="24sp"
        android:layout_margin="16dp"/>

    <GridLayout
        android:layout_below="@id/editTextNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="5"
        android:columnCount="4">

        <Button android:id="@+id/button1" android:text="1" />
        <Button android:id="@+id/button2" android:text="2" />
        <Button android:id="@+id/button3" android:text="3" />
        <Button android:id="@+id/buttonPlus" android:text="+" />

        <Button android:id="@+id/button4" android:text="4" />
        <Button android:id="@+id/button5" android:text="5" />
        <Button android:id="@+id/button6" android:text="6" />
        <Button android:id="@+id/buttonMinus" android:text="-" />

        <Button android:id="@+id/button7" android:text="7" />
        <Button android:id="@+id/button8" android:text="8" />
        <Button android:id="@+id/button9" android:text="9" />
        <Button android:id="@+id/buttonMultiply" android:text="*" />

        <Button android:id="@+id/buttonC" android:text="C" />
        <Button android:id="@+id/button0" android:text="0" />
        <Button android:id="@+id/buttonEquals" android:text="=" />
        <Button android:id="@+id/buttonDivide" android:text="/" />
    </GridLayout>
</RelativeLayout>

MainActivity代码

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity {

    private EditText editTextNumber;
    private String operator = "";
    private double firstValue = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextNumber = findViewById(R.id.editTextNumber);

        int[] buttonIds = {
                R.id.button1, R.id.button2, R.id.button3, R.id.buttonPlus,
                R.id.button4, R.id.button5, R.id.button6, R.id.buttonMinus,
                R.id.button7, R.id.button8, R.id.button9, R.id.buttonMultiply,
                R.id.buttonC, R.id.button0, R.id.buttonEquals, R.id.buttonDivide
        };

        for (int id : buttonIds) {
            Button button = findViewById(id);
            button.setOnClickListener(new ButtonClickListener());
        }
    }

    private class ButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Button button = (Button) v;
            String buttonText = button.getText().toString();

            switch (buttonText) {
                case "C":
                    editTextNumber.setText("");
                    firstValue = 0;
                    operator = "";
                    break;
                case "=":
                    double secondValue = Double.parseDouble(editTextNumber.getText().toString());
                    double result = calculate(firstValue, secondValue, operator);
                    editTextNumber.setText(String.valueOf(result));
                    operator = "";
                    break;
                case "+":
                case "-":
                case "*":
                case "/":
                    firstValue = Double.parseDouble(editTextNumber.getText().toString());
                    operator = buttonText;
                    editTextNumber.setText("");
                    break;
                default:
                    editTextNumber.append(buttonText);
                    break;
            }
        }
    }

    private double calculate(double firstValue, double secondValue, String operator) {
        switch (operator) {
            case "+":
                return firstValue + secondValue;
            case "-":
                return firstValue - secondValue;
            case "*":
                return firstValue * secondValue;
            case "/":
                return secondValue != 0 ? firstValue / secondValue : 0;
            default:
                return 0;
        }
    }
}

好的,下面是制作简易计算器的步骤: 1. 打开Android Studio,创建一个新项目。 2. 在布局文件中添加一个EditText用于显示计算结果和多个Button用于输入数字和运算符。 3. 在MainActivity中获取EditText和Button的引用,并设置Button的点击事件。 4. 在点击事件中,根据按钮的标识符进行不同的计算,并将结果显示到EditText中。 5. 完成以上步骤后,就可以运行程序并测试计算器的功能了。 下面是一个简单的例子代码,供参考: activity_main.xml: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity"> <EditText android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginTop="16dp" android:hint="0" android:padding="16dp" android:textAlignment="textEnd" android:textSize="24sp" /> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:rowCount="4" android:columnCount="4" android:layout_below="@id/result"> <Button android:id="@+id/button_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" android:textSize="24sp" /> <Button android:id="@+id/button_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" android:textSize="24sp" /> <Button android:id="@+id/button_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" android:textSize="24sp" /> <Button android:id="@+id/button_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="+" android:textSize="24sp" /> <Button android:id="@+id/button_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" android:textSize="24sp" /> <Button android:id="@+id/button_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" android:textSize="24sp" /> <Button android:id="@+id/button_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" android:textSize="24sp" /> <Button android:id="@+id/button_subtract" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="-" android:textSize="24sp" /> <Button android:id="@+id/button_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" android:textSize="24sp" /> <Button android:id="@+id/button_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" android:textSize="24sp" /> <Button android:id="@+id/button_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" android:textSize="24sp" /> <Button android:id="@+id/button_multiply" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" android:textSize="24sp" /> <Button android:id="@+id/button_clear" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:layout_gravity="fill" android:text="C" android:textSize="24sp" /> <Button android:id="@+id/button_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" android:textSize="24sp" /> <Button android:id="@+id/button_equal" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="=" android:textSize="24sp" /> <Button android:id="@+id/button_divide" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="/" android:textSize="24sp" /> </GridLayout> </RelativeLayout> ``` MainActivity.java: ``` public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText result; private Button button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9; private Button button_add, button_subtract, button_multiply, button_divide, button_clear, button_equal; private boolean isAddition, isSubtraction, isMultiplication, isDivision; private float value1, value2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = findViewById(R.id.result); button_0 = findViewById(R.id.button_0); button_1 = findViewById(R.id.button_1); button_2 = findViewById(R.id.button_2); button_3 = findViewById(R.id.button_3); button_4 = findViewById(R.id.button_4); button_5 = findViewById(R.id.button_5); button_6 = findViewById(R.id.button_6); button_7 = findViewById(R.id.button_7); button_8 = findViewById(R.id.button_8); button_9 = findViewById(R.id.button_9); button_add = findViewById(R.id.button_add); button_subtract = findViewById(R.id.button_subtract); button_multiply = findViewById(R.id.button_multiply); button_divide = findViewById(R.id.button_divide); button_clear = findViewById(R.id.button_clear); button_equal = findViewById(R.id.button_equal); button_0.setOnClickListener(this); button_1.setOnClickListener(this); button_2.setOnClickListener(this); button_3.setOnClickListener(this); button_4.setOnClickListener(this); button_5.setOnClickListener(this); button_6.setOnClickListener(this); button_7.setOnClickListener(this); button_8.setOnClickListener(this); button_9.setOnClickListener(this); button_add.setOnClickListener(this); button_subtract.setOnClickListener(this); button_multiply.setOnClickListener(this); button_divide.setOnClickListener(this); button_clear.setOnClickListener(this); button_equal.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.button_0: result.setText(result.getText() + "0"); break; case R.id.button_1: result.setText(result.getText() + "1"); break; case R.id.button_2: result.setText(result.getText() + "2"); break; case R.id.button_3: result.setText(result.getText() + "3"); break; case R.id.button_4: result.setText(result.getText() + "4"); break; case R.id.button_5: result.setText(result.getText() + "5"); break; case R.id.button_6: result.setText(result.getText() + "6"); break; case R.id.button_7: result.setText(result.getText() + "7"); break; case R.id.button_8: result.setText(result.getText() + "8"); break; case R.id.button_9: result.setText(result.getText() + "9"); break; case R.id.button_add: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isAddition = true; result.setText(null); } break; case R.id.button_subtract: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isSubtraction = true; result.setText(null); } break; case R.id.button_multiply: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isMultiplication = true; result.setText(null); } break; case R.id.button_divide: if (result == null) { result.setText(""); } else { value1 = Float.parseFloat(result.getText() + ""); isDivision = true; result.setText(null); } break; case R.id.button_clear: result.setText(""); break; case R.id.button_equal: value2 = Float.parseFloat(result.getText() + ""); if (isAddition) { result.setText(value1 + value2 + ""); isAddition = false; } if (isSubtraction) { result.setText(value1 - value2 + ""); isSubtraction = false; } if (isMultiplication) { result.setText(value1 * value2 + ""); isMultiplication = false; } if (isDivision) { result.setText(value1 / value2 + ""); isDivision = false; } break; } } } ``` 这样,一个简单的计算器就完成了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值