Кнопка Backspace в Android-калькуляторе

Любая идея, как проиллюстрировать функцию возврата в этом коде? Я пытаюсь внести некоторые изменения, но функция возврата не работает. Итак, я хотел бы помочь мне с кнопкой возврата.

enter code here открытый класс MainActivity расширяет AppCompatActivity, реализует OnClickListener {

private TextView mCalculatorDisplay;
private Boolean userIsInTheMiddleOfTypingANumber = false;
private CalculatorBrain mCalculatorBrain;
private static final String DIGITS = "0123456789.";


DecimalFormat df = new DecimalFormat("@###########");

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

    // hide the window title.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // hide the status bar and other OS-level chrome
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCalculatorBrain = new CalculatorBrain();
    mCalculatorDisplay = (TextView) findViewById(R.id.textView1);

    df.setMinimumFractionDigits(0);
    df.setMinimumIntegerDigits(1);
    df.setMaximumIntegerDigits(8);

    findViewById(R.id.button0).setOnClickListener(this);
    findViewById(R.id.button1).setOnClickListener(this);
    findViewById(R.id.button2).setOnClickListener(this);
    findViewById(R.id.button3).setOnClickListener(this);
    findViewById(R.id.button4).setOnClickListener(this);
    findViewById(R.id.button5).setOnClickListener(this);
    findViewById(R.id.button6).setOnClickListener(this);
    findViewById(R.id.button7).setOnClickListener(this);
    findViewById(R.id.button8).setOnClickListener(this);
    findViewById(R.id.button9).setOnClickListener(this);
    findViewById(R.id.buttonBackspace).setOnClickListener(this);
    findViewById(R.id.buttonAdd).setOnClickListener(this);
    findViewById(R.id.buttonSubtract).setOnClickListener(this);
    findViewById(R.id.buttonMultiply).setOnClickListener(this);
    findViewById(R.id.buttonDivide).setOnClickListener(this);
    findViewById(R.id.buttonToggleSign).setOnClickListener(this);
    findViewById(R.id.buttonDecimalPoint).setOnClickListener(this);
    findViewById(R.id.buttonEquals).setOnClickListener(this);
    findViewById(R.id.buttonClear).setOnClickListener(this);

    // The following buttons only exist in layout-land (Landscape mode) and require extra attention.
    // The messier option is to place the buttons in the regular layout too and set android:visibility="invisible".
    if (findViewById(R.id.buttonSquareRoot) != null) {
        findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
    }
    if (findViewById(R.id.buttonSquared) != null) {
        findViewById(R.id.buttonSquared).setOnClickListener(this);
    }
    if (findViewById(R.id.buttonInvert) != null) {
        findViewById(R.id.buttonInvert).setOnClickListener(this);
    }
    if (findViewById(R.id.buttonSine) != null) {
        findViewById(R.id.buttonSine).setOnClickListener(this);
    }
    if (findViewById(R.id.buttonCosine) != null) {
        findViewById(R.id.buttonCosine).setOnClickListener(this);
    }
    if (findViewById(R.id.buttonTangent) != null) {
        findViewById(R.id.buttonTangent).setOnClickListener(this);
    }

}


    @Override
    public void onClick (View v) {
        String buttonPressed = ((Button) v).getText().toString();

        if (DIGITS.contains(buttonPressed)) {

            // digit was pressed
            if (userIsInTheMiddleOfTypingANumber) {

                if (buttonPressed.equals(".") && mCalculatorDisplay.getText().toString().contains(".")) {
                    // ERROR PREVENTION
                    // Eliminate entering multiple decimals
                } else {
                    mCalculatorDisplay.append(buttonPressed);
                }

            } else {

                if (buttonPressed.equals(".")) {
                    // ERROR PREVENTION
                    // This will avoid error if only the decimal is hit before an operator, by placing a leading zero
                    // before the decimal
                    mCalculatorDisplay.setText(0 + buttonPressed);
                } else {
                    mCalculatorDisplay.setText(buttonPressed);
                }


            }
                userIsInTheMiddleOfTypingANumber = true;


            }else{
                // operation was pressed
                if (userIsInTheMiddleOfTypingANumber) {

                    mCalculatorBrain.setOperand(Double.parseDouble(mCalculatorDisplay.getText().toString()));
                    userIsInTheMiddleOfTypingANumber = false;
                }

                mCalculatorBrain.performOperation(buttonPressed);

                if (new Double(mCalculatorBrain.getResult()).equals(0.0)) {
                    mCalculatorDisplay.setText("" + 0);
                } else {
                    mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
                }

            }
        }

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // Save variables on screen orientation change
    outState.putDouble("OPERAND", mCalculatorBrain.getResult());
    outState.putDouble("MEMORY", mCalculatorBrain.getMemory());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
    // Restore variables on screen orientation change
    mCalculatorBrain.setOperand(savedInstanceState.getDouble("OPERAND"));
    mCalculatorBrain.setMemory(savedInstanceState.getDouble("MEMORY"));
    if (new Double(mCalculatorBrain.getResult()).equals(0.0)){
        mCalculatorDisplay.setText("" + 0);
    } else {
        mCalculatorDisplay.setText(df.format(mCalculatorBrain.getResult()));
    }
}

}


person Norman    schedule 04.11.2015    source источник
comment
Вы имеете в виду, как обнаружить кнопку возврата или как выполнить логику удаления?   -  person Xiaoyu Yu    schedule 04.11.2015
comment
Правильно, как обнаружить кнопку возврата?   -  person Norman    schedule 05.11.2015


Ответы (1)


В своем макете вы можете добавить атрибут onClick к каждой кнопке, скажем, onClick="function", и в вашей деятельности вам просто нужно реализовать такой метод:

public void function(View v) {
    switch(v.getId()) {
        case R.id.buttonBackspace:
            // handle the backspace button
            break;
        case R.id.xxx:
            // handle the button
            break;
        ...
    }
}

А для цифр я предлагаю назначить тег каждой кнопке с цифрой в макете и сделать свою логику в java на основе тега, а не текста на кнопке. Поскольку текст — это всего лишь пользовательский интерфейс, он может измениться в будущем из-за других возможных требований.

person Xiaoyu Yu    schedule 04.11.2015