Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带一个输入文本的计算器_Android - Fatal编程技术网

Android 带一个输入文本的计算器

Android 带一个输入文本的计算器,android,Android,我是android的初学者。我正试图使一个计算器,只有一个输入编辑文本。 当我点击+按钮时,它不会给出总和输出。要获得正确的ans,我必须单击两个条目后的+按钮。想得到一个和,我会做1“+”1“+”=。然后它会得到2。这是我的代码,有人请帮助我 public void onClick(View v){ double sum=0; switch(v.getId()){ case R.id.buttonplus: sum += Double.parseDou

我是android的初学者。我正试图使一个计算器,只有一个输入编辑文本。 当我点击+按钮时,它不会给出总和输出。要获得正确的ans,我必须单击两个条目后的+按钮。想得到一个和,我会做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;
    }

如果我理解正确,您希望在按下“等于”
按钮后显示总和。如果是,则需要

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;
当您按下“加号”按钮时,第二个数字还没有输入,因此总和只是第一个数字。然后您必须再次按下它才能添加到
sum

所以在if等于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);
        }
    }

欢迎来到SO。你有什么问题吗?我正在尝试制作一个计算器,但是按钮不能正常工作。我上面的代码显示了两个加号和相等的情况。所以两个加两个数。我必须在我要加的两个数之后按加号按钮,然后它会给我一个ans。但是我希望只需单击一下,所有的按钮都可以。但是如果我想对每个按钮都这样做,比如减法按钮,乘法按钮,除法按钮,然后我该怎么做。有不同的方法来处理这个问题。当我回到家的时候,我必须看看很久以前我是怎么做的,但我想我一直在跟踪上次使用的运算符,并将其传递给“equals”“
按钮
但是如果我按照你说的那样做,那么它将进入一个新的情况,sum的值将再次变为0。如果你在调用resultView.setText()之前不设置
sum=0
,那么它就不重要了