我在计算器应用中遇到问题-如何解决?

时间:2018-10-22 08:15:59

标签: java android

我遇到的问题是如何通过按新操作来更改上一个操作,新操作将覆盖上一个操作

package com.zpat.calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity
        extends AppCompatActivity {

    private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, bdot, bans, bclear;
    private TextView ans, Info;
    private final char ADDITION = '+';
    private final char SUBTRACTION = '-';
    private final char MULTIPLICATION = '*';
    private final char DIVISION = '/';
    private final char EQUAL = 0;
    private String val1 = "";
    private String val2 = "";
    private char Action;
    boolean isAnsPress = false;
    boolean isDotPressed = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupUIViews();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "1");
                }
            }
        });

        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "2");
                }
            }
        });

        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "3");
                }
            }
        });

        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "4");
                }
            }
        });

        b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "5");
                }
            }
        });

        b6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "6");
                }
            }
        });

        b7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "7");
                }
            }
        });

        b8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "8");
                }
            }
        });

        b9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String a = ans.getText().toString();
                if (a.length() <= 8) {
                    ans.setText(ans.getText().toString() + "9");
                }
            }
        });

        b0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String a = ans.getText().toString();
                if (a.length() >= 1) {
                    TextUtils.isEmpty(a);
                }
                ans.setText(ans.getText().toString() + "0");

            }
        });

        bdot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!isDotPressed) {
                    ans.setText(ans.getText() + ".");
                    isDotPressed = true;
                }
            }
        });

        badd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String a = Info.getText().toString().substring(Info.getText().toString().length() - 1, Info.getText().toString().length());

                    if (Action == ADDITION) {

                        compute();

                        Info.setText(String.valueOf(val1) + "+");

                        ans.setText(null);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                isAnsPress = false;
                isDotPressed = false;
            }
        });
        bsub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    compute();
                    Action = SUBTRACTION;
                    Info.setText(String.valueOf(val1) + "-");
                    ans.setText(null);
                    isAnsPress = false;
                    isDotPressed = false;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        bmul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    compute();
                    Action = MULTIPLICATION;
                    Info.setText(String.valueOf(val1) + "*");
                    ans.setText(null);
                    isAnsPress = false;
                    isDotPressed = false;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });
        bdiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    compute();
                    Action = DIVISION;
                    Info.setText(String.valueOf(val1) + "/");
                    ans.setText(null);
                    isAnsPress = false;
                    isDotPressed = false;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        bans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!isAnsPress) {
                        compute();
                        Action = EQUAL;
                        Info.setText(Info.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
                        ans.setText(null);
                        isAnsPress = true;
                        isDotPressed = false;
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });
        bclear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String a = ans.getText().toString();
                if (ans.getText().length() > 0) {
                    CharSequence name = ans.getText().toString();
                    ans.setText(name.subSequence(0, name.length() - 1));
                } else {
                    Info.setText(null);
                    val1 = "";
                }
                isAnsPress = false;
                isDotPressed = false;
            }
        });
    }

    private void setupUIViews() {
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.btn1);
        b2 = (Button) findViewById(R.id.btn_2);
        b3 = (Button) findViewById(R.id.btn_3);
        b4 = (Button) findViewById(R.id.btn4);
        b5 = (Button) findViewById(R.id.btn_5);
        b6 = (Button) findViewById(R.id.btn_6);
        b7 = (Button) findViewById(R.id.btn_7);
        b8 = (Button) findViewById(R.id.btn_8);
        b9 = (Button) findViewById(R.id.btn_9);
        b0 = (Button) findViewById(R.id.btn_0);
        badd = (Button) findViewById(R.id.btn_Add);
        bsub = (Button) findViewById(R.id.btn_Sub);
        bmul = (Button) findViewById(R.id.btn_Multiply);
        bdiv = (Button) findViewById(R.id.btn_div);
        bdot = (Button) findViewById(R.id.btn_dot);
        bans = (Button) findViewById(R.id.btn_equal);
        bclear = (Button) findViewById(R.id.btn_Clear);
        ans = (TextView) findViewById(R.id.Answer);
        Info = (TextView) findViewById(R.id.info);

    }

    private void compute() {
        if (val1 != "") {
            val2 = ans.getText().toString();
            switch (Action) {
                case ADDITION:
                    val1 = String.valueOf(Double.parseDouble(val1) + Double.parseDouble(val2));
                    break;
                case SUBTRACTION:
                    val1 = String.valueOf(Double.parseDouble(val1) - Double.parseDouble(val2));
                    break;
                case MULTIPLICATION:
                    val1 = String.valueOf(Double.parseDouble(val1) * Double.parseDouble(val2));
                    break;
                case DIVISION:
                    val1 = String.valueOf(Double.parseDouble(val1) / Double.parseDouble(val2));
                    break;
                case EQUAL:
                    break;
            }
        } else {
            val1 = ans.getText().toString();
        }
        double temp = Double.parseDouble(val1);
        int temp2 = (int) temp;
        double temp3 = temp - temp2;
        if (temp3 == 0) {
            val1 = String.valueOf((int) Double.parseDouble(val1));
        }

    }
}

enter image description here

0 个答案:

没有答案