0

私はアンドロイドが初めてなので、最初のアプリとして電卓を作ろうとしていました。すべてを実行しましたが、問題が 1 つだけあります。値が 0 の場合、先頭に 0 があります。[クリア] をクリックすると、0.0 と表示されます。0 に変更したいのですが、変更方法がわかりません。

主な活動

public class MainActivity extends Activity implements OnClickListener {

private TextView calculatorDisplay;
private static final String DIGITS = "0123456789.";
private Boolean userIsInTheMiddleOfTypingANumber = false;

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

CalculatorBrain brain;

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

    brain = new CalculatorBrain();

    calculatorDisplay = (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.buttonAdd).setOnClickListener(this);
    findViewById(R.id.buttonSubtract).setOnClickListener(this);
    findViewById(R.id.buttonMultiply).setOnClickListener(this);
    findViewById(R.id.buttonDivide).setOnClickListener(this);
    findViewById(R.id.buttonSquareRoot).setOnClickListener(this);
    findViewById(R.id.buttonInvert).setOnClickListener(this);
    findViewById(R.id.buttonCos).setOnClickListener(this);
    findViewById(R.id.buttonSin).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);
    findViewById(R.id.buttonClearMemory).setOnClickListener(this);
    findViewById(R.id.buttonAddToMemory).setOnClickListener(this);
    findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this);
    findViewById(R.id.buttonRecallMemory).setOnClickListener(this);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.hide();
    }
}

// @Override
public void onClick(View view) {

    String buttonPressed = ((Button) view).getText().toString();
    // String digits = "0123456789.";

    if (DIGITS.contains(buttonPressed)) {
        // digit was pressed
        if (userIsInTheMiddleOfTypingANumber) {
            calculatorDisplay.append(buttonPressed);
        } else {
            calculatorDisplay.setText(buttonPressed);
            userIsInTheMiddleOfTypingANumber = true;
        }
    } else {
        // operation was pressed
        if (userIsInTheMiddleOfTypingANumber) {
            brain.setOperand(Double.parseDouble(calculatorDisplay.getText().toString()));
            userIsInTheMiddleOfTypingANumber = false;
        }

        brain.performOperation(buttonPressed);
        calculatorDisplay.setText(df.format(brain.getResult()));

    }
}

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

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // Restore variables on screen orientation change
    brain.setOperand(savedInstanceState.getDouble("OPERAND"));
    brain.setMemory(savedInstanceState.getDouble("MEMORY"));
    calculatorDisplay.setText(df.format(brain.getResult()));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

電卓頭脳

public class CalculatorBrain {
// 3 + 6 = 9
// 3 & 6 are called the operand.
// The + is called the operator.
// 9 is the result of the operation.
private double operand = 0;
private double waitingOperand = 0;
private String waitingOperator = "";
private double calculatorMemory = 0;

public void setOperand(double operand) {
    this.operand = operand;
}

public double getResult() {
    return operand;
}

// used on screen orientation change
public void setMemory(double calculatorMemory) {
    this.calculatorMemory = calculatorMemory;
}

// used on screen orientation change
public double getMemory() {
    return calculatorMemory;
}

public String toString() {
    return Double.toString(operand);
}

protected double performOperation(String operator) {

    /*
    * If you are using Java 7, then you can use switch in place of if statements
    *
    *     switch (operator) {
    *     case "MC":
    *         calculatorMemory = 0;
    *         break;
    *     case "M+":
    *         calculatorMemory = calculatorMemory + operand;
    *         break;
    *     }
    */

    if (operator.equals("MC")) {
        calculatorMemory = 0;
    } else if (operator.equals("M+")) {
        calculatorMemory = calculatorMemory + operand;
    } else if (operator.equals("M-")) {
        calculatorMemory = calculatorMemory - operand;
    } else if (operator.equals("MR")) {
        operand = calculatorMemory;
    } else if (operator.equals("C")) {
        operand = 0;
        waitingOperator = "";
        waitingOperand = 0;
        calculatorMemory = 0;
    } else if (operator.equals("Sqrt")) {
        operand = Math.sqrt(operand);
    } else if (operator.equals("1/x")) {
        if (operand != 0) {
            operand = 1 / operand;
        }
    } else if (operator.equals("+/-")) {
        operand = -operand;
    } else if (operator.equals("sin")) {
        operand = Math.sin(operand);
    } else if (operator.equals("cos")) {
        operand = Math.cos(operand);
    } else {
        performWaitingOperation();
        waitingOperator = operator;
        waitingOperand = operand;
    }

    return operand;
}

protected void performWaitingOperation() {

    if (waitingOperator.equals("+")) {
        operand = waitingOperand + operand;
    } else if (waitingOperator.equals("*")) {
        operand = waitingOperand * operand;
    } else if (waitingOperator.equals("-")) {
        operand = waitingOperand - operand;
    } else if (waitingOperator.equals("/")) {
        if (operand != 0) {
            operand = waitingOperand / operand;
        }
    }

}
4

2 に答える 2

0

これは、 がgetResult()a を返すために発生していdoubleます。

0の代わりに を表示したい場合は0.0、次のようにチェックします。

交換:

calculatorDisplay.setText(df.format(brain.getResult()));

と:

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

これで問題が解決するはずです。

編集1:

次の変更を試してください。

if (DIGITS.contains(buttonPressed)) {
    // digit was pressed
    if (userIsInTheMiddleOfTypingANumber) {
        calculatorDisplay.append(buttonPressed);
    } else {            
        if (buttonPressed.equals("0") && !calculatorDisplay.getText().toString().equals("0")) {
            calculatorDisplay.setText("0");
        } else if (!buttonPressed.equals("0")) {
            if (buttonPressed.equals(".")) {
                calculatorDisplay.setText("0.");
            } else {
                calculatorDisplay.setText(buttonPressed);
            }
            userIsInTheMiddleOfTypingANumber = true;
        }
    }
}
于 2013-07-19T19:44:29.320 に答える
0

テキスト ビューでテキスト ウォッチャー リスナーを使用する必要があります。

textview.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {

        // TODO Auto-generated method stub
            if(textview.gettText().toString().equals("0"))
            {
                    textview.settText("0.0");
            }
    }
});
于 2013-07-19T19:39:16.583 に答える