安卓开发——简易计算器

一,界面设计

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

    <TextView
        android:id="@+id/Result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_marginLeft="4px"
        android:gravity="left"
        android:text="0"
        android:textSize="50dip"/>
    <Button
        android:id="@+id/clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp" />
    <Button android:id="@+id/num1" android:text="1" android:textSize="26sp" />
    <Button android:id="@+id/num2" android:text="2" android:textSize="26sp" />
    <Button android:id="@+id/num3" android:text="3" android:textSize="26sp" />
    <Button android:id="@+id/plus" android:text="+" android:textSize="26sp" />
    <Button android:id="@+id/num4" android:text="4" android:textSize="26sp" />
    <Button android:id="@+id/num5" android:text="5" android:textSize="26sp" />
    <Button android:id="@+id/num6" android:text="6" android:textSize="26sp" />
    <Button android:id="@+id/subtract" android:text="-" android:textSize="26sp" />
    <Button android:id="@+id/num7" android:text="7" android:textSize="26sp" />
    <Button android:id="@+id/num8" android:text="8" android:textSize="26sp" />
    <Button android:id="@+id/num9" android:text="9" android:textSize="26sp" />
    <Button android:id="@+id/multiply" android:text="*" android:textSize="26sp" />
    <Button android:id="@+id/point" android:text="." android:textSize="26sp" />
    <Button android:id="@+id/num0" android:text="0" android:textSize="26sp" />
    <Button android:id="@+id/equal" android:text="=" android:textSize="26sp" />
    <Button android:id="@+id/divide" android:text="/" android:textSize="26sp" />
</GridLayout>

 二,代码设计

package com.example.jisuanqi;

        import android.app.Activity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextView;

public class MainActivity extends Activity {


    private Button[] btnNum = new Button[11];
    private Button[] btnOperator = new Button[5];
    private TextView textView = null;
    private Button btnClear = null;

    private static String operator;
    private boolean clearFlag;
    private boolean firstFlag;
    private double result;
    public MainActivity() {

        result = 0;
        firstFlag = true;
        clearFlag = false;
        operator = "=";
    }

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


        btnOperator[0] = (Button) findViewById(R.id.plus);
        btnOperator[1] = (Button) findViewById(R.id.subtract);
        btnOperator[2] = (Button) findViewById(R.id.multiply);
        btnOperator[3] = (Button) findViewById(R.id.divide);
        btnOperator[4] = (Button) findViewById(R.id.equal);

        btnNum[0] = (Button) findViewById(R.id.num0);
        btnNum[1] = (Button) findViewById(R.id.num1);
        btnNum[2] = (Button) findViewById(R.id.num2);
        btnNum[3] = (Button) findViewById(R.id.num3);
        btnNum[4] = (Button) findViewById(R.id.num4);
        btnNum[5] = (Button) findViewById(R.id.num5);
        btnNum[6] = (Button) findViewById(R.id.num6);
        btnNum[7] = (Button) findViewById(R.id.num7);
        btnNum[8] = (Button) findViewById(R.id.num8);
        btnNum[9] = (Button) findViewById(R.id.num9);
        btnNum[10] = (Button) findViewById(R.id.point);


        textView=(TextView) findViewById(R.id.Result);
        textView.setText("0.0");

        NumberAction na = new NumberAction();
        OperatorAction oa = new OperatorAction();
        for (Button bc : btnOperator) {
            bc.setOnClickListener((View.OnClickListener) oa);
        }
        for (Button bc : btnNum) {
            bc.setOnClickListener((View.OnClickListener) na);
        }

        btnClear = (Button) findViewById(R.id.clear);
        btnClear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                textView.setText("0.0");

                result = 0;
                firstFlag = true;
                clearFlag = false;
                operator = "=";

            }
        });
    }


    private class NumberAction implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Button btn = (Button) view;
            String input = btn.getText().toString();


            if (firstFlag) {
                if (input.equals(".")) {
                    return;
                }

                if (textView.getText().toString().equals("0.0")) {
                    textView.setText("");
                }
                firstFlag = false;
            } else {
                String textViewStr = textView.getText().toString();

                if (textViewStr.indexOf(".") != -1 && input.equals(".")) {
                    return;
                }

                if (textViewStr.equals("-") && input.equals(".")) {
                    return;
                }

                if (textViewStr.equals("0") && !input.equals(".")) {
                    return;
                }
            }


            if (clearFlag) {
                textView.setText("");
                clearFlag = false;
            }
            textView.setText(textView.getText().toString() + input);
        }
    }

    private class OperatorAction implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Button btn = (Button) view;
            String inputOperator = (String) btn.getText();

            if (firstFlag) {
                if (inputOperator.equals("-")) {
                    textView.setText("-");
                    firstFlag = false;
                }
            } else {

                if (!clearFlag) {

                    calculate(Double.parseDouble(textView.getText().toString()));
                }

                operator = inputOperator;
                clearFlag = true;
            }
        }
    }


    public void calculate(double x) {

        if (operator.equals("+")) {
            result += x;
        } else if (operator.equals("-")) {
            result -= x;
        } else if (operator.equals("*")) {
            result *= x;
        } else if (operator.equals("/")) {
            result /= x;
        } else if (operator.equals("=")) {
            result = x;
        }
        textView.setText("" + result);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚睡晚起秦叁叁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值