我正在建立一个使用两个textViews作为输入和一个textView作为结果框的计算器。当我按+,-,x或/按钮时,程序将切换到textView2并继续输入数字。然后,将单击=按钮以执行计算。

当我单击=按钮时,程序会忘记我按了+,-,x或/按钮,因此它不知道应如何计算。

有没有一种方法可以“记住”第一个按钮的单击,以便可以使用它来确定要计算的内容?


  Android代码已更新,V2


    package com.example.tristan.assn2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    TextView operand1, operand2,result,tv;
    int operation;

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

        operand1 = (TextView) findViewById(R.id.textView);
        operand2 = (TextView) findViewById(R.id.textView2);

        result= (TextView) findViewById(R.id.textView3);
        tv=operand1;
    }

    public void sendMessage(View v) {
        if (v.getId() == R.id.button18 || v.getId() == R.id.button15 || v.getId() == R.id.button17 || v.getId() == R.id.button21 || v.getId() == R.id.button18) {
            tv=operand2;
        }

        if (v.getId() == R.id.button) {
            tv.setText(tv.getText() + "1");
        }
        if (v.getId() == R.id.button2) {
            tv.setText(tv.getText() + "2");
        }
        if (v.getId() == R.id.button3) {
            tv.setText(tv.getText() + "3");
        }
        if (v.getId() == R.id.button4) {
            tv.setText(tv.getText() + "4");
        }
        if (v.getId() == R.id.button5) {
            tv.setText(tv.getText() + "5");
        }
        if (v.getId() == R.id.button6) {
            tv.setText(tv.getText() + "6");
        }
        if (v.getId() == R.id.button7) {
            tv.setText(tv.getText() + "7");
        }
        if (v.getId() == R.id.button8) {
            tv.setText(tv.getText() + "8");
        }
        if (v.getId() == R.id.button9) {
            tv.setText(tv.getText() + "9");
        }
        if (v.getId() == R.id.button10) {
            tv.setText(tv.getText() + "0");
        }
        if (v.getId() == R.id.button11) {
            tv.setText(tv.getText() + ".");
        }




        Button plus =  (Button) findViewById(R.id.button17);
        Button minus =  (Button) findViewById(R.id.button15);
        Button mult =  (Button) findViewById(R.id.button18);
        Button div =  (Button) findViewById(R.id.button21);

            //plus
        if(v.getId()==R.id.button17){
            operation = 1;
        }
        //minus
        if(v.getId()==R.id.button15){
            operation = 2;
        }
        //mult
        if(v.getId()==R.id.button18){
            operation = 3;
        }
        //div
        if(v.getId()==R.id.button21){
            operation = 4;
        }


        int op1, op2;
        switch(operation){
            case 1:
                op1=Integer.parseInt(operand1.getText().toString());
                op2=Integer.parseInt(operand2.getText().toString());
                op2+=op1;
                if(v.getId()==R.id.button23){
                    result.setText(op2+"");
                    tv=operand1;
                }
                break;
            case 2:
                op1=Integer.parseInt(operand1.getText().toString());
                op2=Integer.parseInt(operand2.getText().toString());
                op2-=op1;
                if(v.getId()==R.id.button23){
                    result.setText(op2+"");
                    tv=operand1;
                }
                break;
            case 3:
                op1=Integer.parseInt(operand1.getText().toString());
                op2=Integer.parseInt(operand2.getText().toString());
                op2*=op1;
                if(v.getId()==R.id.button23){
                    result.setText(op2+"");
                    tv=operand1;
                }
                break;
            case 4:
                op1=Integer.parseInt(operand1.getText().toString());
                op2=Integer.parseInt(operand2.getText().toString());
                op2/=op1;
                if(v.getId()==R.id.button23){
                    result.setText(op2+"");
                    tv=operand1;
                }
                break;
        }

    }
}



  如何分配操作?


    //plus
    if(v.getId()==R.id.button17){
        operation = 1;
    }
    //minus
    if(v.getId()==R.id.button15){
        operation = 2;
    }
    //mult
    if(v.getId()==R.id.button18){
        operation = 3;
    }
    //div
    if(v.getId()==R.id.button21){
        operation = 4;
    }

最佳答案

private String operation;


在每个操作的输出中,您将编写:
对于加:

operation = "+";


减去:

operation = "-";


对于div:

operation = "/";


等等...
如果选中,查询变量操作。
切换或。

switch (operation) {
    case "+":
        //Your operation
        break;
    case "-":
        //Your operation
        break;
    case "/":
        //Your operation
        break;
}

09-04 19:19
查看更多