2

我是android的初学者。我正在尝试用一个输入编辑文本制作一个计算器。当我单击 + 按钮时,它不会给出总和输出。要获得正确的答案,我必须在两个条目后单击 + 按钮。想得到一个总和,我将它作为 1"+" 1"+""=。然后它会给出 2。这是我的代码,请有人帮助我。

public void onClick(View v){
    double sum=0; 
    switch(v.getId()){
    case R.id.buttonplus:
        sum += Double.parseDouble(String.valueOf(textView.getText()));
        numberDisplayed.delete(0,numberDisplayed.length());
        break;
    case R.id.buttonequal:
        resultView.setText(String.valueOf(sum));
        sum=0;
    }
4

1 回答 1

2

如果我理解正确,您希望在按下 "equals" 后显示总和button。如果是这样,那么你需要有

sum += Double.parseDouble(String.valueOf(textView.getText()));

在这一行也

case R.id.buttonequal:
    sum += Double.parseDouble(String.valueOf(textView.getText()));
    resultView.setText(String.valueOf(sum));
    sum=0;

当您按下“加号”时,第二个数字尚未输入,button因此总和只是第一个数字。然后你必须再次按下它才能添加到sum

所以在 if equals btn 按下时,类似

if (lastOp.equals("sub")
{
    sum -=  Double.parseDouble(String.valueOf(textView.getText()));
...
}

例子

public class SimpleCalculatorActivity extends Activity
{
    //variables needing class scope
    double answer = 0, number1, number2;
    int operator = 0, number;
    boolean hasChanged = false, flag = false;
    String display = null;
    String display2 = null;
    String curDisplay = null;
    String calcString = "";
    String inputLabel;  
    String inputString = null;
    String inputString2 = null;
    String inputString3 = null;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("Super Duper Calculator");

        initButtons();      
    }   
        //when button is pressed, send num to calc function
        button1.setOnClickListener
        (new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    inputString = button1.getText().toString();
                    displayCalc(inputString);
                }
            }
        );

        button2.setOnClickListener
        (new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    inputString = button2.getText().toString();
                    displayCalc(inputString);
                }
            }
        );
        ...
        //send operator to calc function
        addButton.setOnClickListener
        (new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    calculation(1);
                }
            }
        );

        subButton.setOnClickListener
        (new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    calculation(2);
                }
            }
        );       
        calcButton.setOnClickListener
        (new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    calculation(5);
                }
            }
        );

        clearButton.setOnClickListener
        (new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    calculation(6);
                }
            }
        );
    }   

    //function to calculate
    public void calculation(int input)
    {
        number = input;

        //see which operator was clicked
        switch (number)
        {
            case 1:
                operator = 1;
                hasChanged = true;
                display = "";
                showDisplay("+");
                break;
            case 2:
                operator = 2;
                hasChanged = true;
                display = "";
                showDisplay("-");               
                break;
            case 3:
                operator = 3;
                hasChanged = true;
                display = "";
                showDisplay("*");
                break;
            case 4:
                operator = 4;
                hasChanged = true;
                display = "";
                showDisplay("/");
                break;
            case 5:
                number2 = Double.parseDouble(display2);
                if(number2 == 0)
                {
                    custErrMsg();
                }
                else
                {
                    operator();
                    displayAnswer(answer);
                    hasChanged = true;
                }
                break;
            case 6:
                clear();
                break;
            default:
                clear();
                break;
        }
    }

    private void operator()
    {
        if (operator != 0) 
        {
            if (operator == 1)
            {
                answer = number1 + number2;
            }
            else if (operator == 2)
            {
                answer = number1 - number2;
            }
            else if (operator == 3)
            {
                answer = number1 * number2;
            }
            else if (operator == 4)
            {
                answer = number1 / (number2);
            }
        }
    }

    private void displayCalc(String curValue)
    {
        String curNum = curValue;

        if (!hasChanged)
        {
            if (display == null)
            {
                //display number if reset
                inputString2 = curNum;
                display = inputString2;    
                showDisplay(display);

            }
            else
            {
                //display previous input + new input
                inputString2 = inputString2 + curNum;
                display =  display + curNum;
                showDisplay(display);
            }
        }
        else                                                    
        {   
            displayNum2(curNum);
        }
    }

    private void displayNum2 (String curValue2)
    {
        String curNum2;
        curNum2 = curValue2;
        if (!flag)
        {
            //display number if reset
            inputString3 = curNum2;
            display2 = inputString3;
            number1 = Double.parseDouble(inputString2);
            flag = true;
        }
        else
        {
            //display previous input + new input
            inputString3 = curNum2;
            display2 = display2 + curNum2;
        }
        showDisplay(inputString3);
    }

    private void displayAnswer(double curAnswer)
    {
        String finAnswer = String.valueOf(curAnswer);  
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setBackgroundColor(0xffffffff);
        textView1.setText(finAnswer);
    }

    private void showDisplay(String output)
    {   
        inputLabel = output;
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setBackgroundColor(0xffffffff);

        if (operator != 0)
        {
            curDisplay = textView1.getText().toString();
            textView1.setText(curDisplay + inputLabel);
        }     
        else
        {
            textView1.setText(inputLabel);
        }
    }
于 2013-03-20T19:29:23.383 回答