Бутон Backspace на калкулатора на Android

Някаква идея как да илюстрирам функцията backspace в този код? Опитвам се да направя някои промени, но не може да работи с функцията Backspace. И така, бих искал да ми помогне с бутона за връщане назад.

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