计算器与一个输入edittext android

时间:2013-03-20 19:14:16

标签: android

我是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;
    }

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望在按下“等于”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

所以如果等于按下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);
        }
    }