如何正确实现计算器擦除按钮?

时间:2016-09-20 13:25:16

标签: java for-loop button calculator backspace

我添加了退格按钮,但是当我点击按钮时工作正常,但当我点击任何按钮时,它会显示我之前删除的所有数字。还请告诉我如何在(1x2+5=?)等计算器中执行多项操作。

当我单击按钮清除(onClickClear)时,它可以工作,但当我点击另一个数字时,将自动显示我删除的最后一个数字。

public class MainActivity extends Activity {
    Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonAdd, buttonSub, buttonMulti, buttonDivide, buttonEqual;
    Button button;
    EditText editText;
    String display = "";
    String result = "";
    String str = "";
    String currentOperator = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        button0 = (Button) findViewById(R.id.button0);
        button9 = (Button) findViewById(R.id.button9);
        button8 = (Button) findViewById(R.id.button8);
        button7 = (Button) findViewById(R.id.button7);
        button6 = (Button) findViewById(R.id.button6);
        button5 = (Button) findViewById(R.id.button5);
        button4 = (Button) findViewById(R.id.button4);
        button3 = (Button) findViewById(R.id.button3);
        button2 = (Button) findViewById(R.id.button2);
        button1 = (Button) findViewById(R.id.button1);
        button = (Button) findViewById(R.id.button);

        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        buttonSub = (Button) findViewById(R.id.buttonSub);
        buttonMulti = (Button) findViewById(R.id.buttonMulti);
        buttonDivide = (Button) findViewById(R.id.buttonDivide);


        editText = (EditText) findViewById(R.id.editText);
        //editText.setText(display);
    }

    public void displayScreen(){
        editText.setText(display);
    }

    public void onClick(View v){
        int id = v.getId();
        if (result != ""){
            display = "";
            currentOperator = "";
            result = "";
            displayScreen();
        }
        switch (id){
            case R.id.button0:
                display += button0.getText();
                displayScreen();
                break;

            case R.id.button1:

                display += button1.getText();
                displayScreen();
                break;

            case R.id.button2:

                display += button2.getText();
                displayScreen();
                break;

            case R.id.button3:

                display += button3.getText();
                displayScreen();
                break;

            case R.id.button4:

                display += button4.getText();
                displayScreen();
                break;

            case R.id.button5:

                display += button5.getText();
                displayScreen();
                break;

            case R.id.button6:

                display += button6.getText();
                displayScreen();
                break;

            case R.id.button7:

                display += button7.getText();
                displayScreen();
                break;

            case R.id.button8:

                display += button8.getText();
                displayScreen();
                break;

            case R.id.button9:

                display += button9.getText();
                displayScreen();
                break;



            default:
                break;


        }
    }


    public void onClickOperator(View v){

        int id = v.getId();
        if(result != "") {
            display = result;
            result = "";
        }

        switch (id){

            case R.id.buttonAdd:

                display += buttonAdd.getText();
                currentOperator = buttonAdd.getText().toString();
                displayScreen();
                break;

            case R.id.buttonSub:

                display += buttonSub.getText();
                currentOperator = buttonSub.getText().toString();
                displayScreen();
                break;

            case R.id.buttonMulti:
                display += buttonMulti.getText();
                currentOperator = buttonMulti.getText().toString();
                displayScreen();
                break;

            case R.id.buttonDivide:
                display += buttonDivide.getText();
                currentOperator = buttonDivide.getText().toString();
                displayScreen();
                break;


            default:
                break;

        }
    }
    // this for delete the digits from the editExt.
    //It works but when i clicked the  then
    //Any number button then it  shows all deleted digits
    public void onClickClear(View v){
        str = editText.getText().toString();
        if (str.length() > 0) {
            str = str.substring(0, str.length()-1);
            editText.setText(str);

        }
    }

    private double calculate(String a, String b, String op){

        switch (op){
            case "+": return Double.valueOf(a) + Double.valueOf(b);
            case "-": return Double.valueOf(a) - Double.valueOf(b);
            case "x": return Double.valueOf(a) * Double.valueOf(b);
            case "/": return Double.valueOf(a) / Double.valueOf(b);

            default:
                return -1;
        }

    }

    public  void onClickEqual(View v){
        String[] operation = display.split(Pattern.quote(currentOperator));
        if(operation.length < 2) return;

        result = String.valueOf(calculate(operation[0],operation[1],currentOperator));
        editText.setText(display + "\n" + String.valueOf(result));

    }


    public void onClear(View v){
        editText.setText(null);
        display = "";
        currentOperator = "";
        result = "";
        displayScreen();
    }

}

1 个答案:

答案 0 :(得分:0)

我想您的问题可能是您要清除editText文本而不是display。因此,当displayScreen()再次被调用时,它只会将display字符串放回editText中,而 public void onClickClear(View v) { editText.getText().clear(); display = ""; } 字符串中仍包含旧内容。

你试过这个:

 public void onClickClear(View v)
 {
     display = "";
     editText.setText(display);
 }

或者这个:

and