It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                7年前关闭。
            
        

我正在尝试在android中对计算器应用进行排序。我的布局很好,现在我只需要执行将要计算的代码即可。该应用程序需要在按下时显示该数字,然后如果说按+按钮,它将存储第一个数字,将其清除在屏幕外,获取第二个数字,然后将它们加在一起。当按下等号按钮时,它需要显示结果。

到目前为止,我有

public class Calculator extends Activity {

    public EditText display;
    TextView screen;
    int a, result;
    int c;
    Button b1;
    int sum1, sum2;
    String r;

    public int getSum1(int sum1) {
        return sum1;
    }

    public int getSum2(int sum2) {
        return sum2;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void buttonEventHandler(View v) {
        screen=(EditText)findViewById(R.id.screen);
        b1=(Button)findViewById(R.id.button1);
        switch(v.getId()) {

        case R.id.button1 :
            screen.setText(screen.getText() + "1");
            c = +1;
            break;

        case R.id.button2 :
            screen.setText(screen.getText() + "2");
            c = +2;
            break;

        case R.id.button3 :
            screen.setText(screen.getText() + "3");
            c = +3;
            break;

        case R.id.button4 :
            screen.setText(screen.getText() + "4");
            c = +4;
            break;

        case R.id.button5 :
            screen.setText(screen.getText() + "5");
            c = +5;
            break;

        case R.id.button6 :
            screen.setText(screen.getText() + "6");
            c = +6;
            break;

        case R.id.button7 :
            screen.setText(screen.getText() + "7");
            c = +7;
            break;

        case R.id.button8 :
            screen.setText(screen.getText() + "8");
            c = +8;
            break;

        case R.id.button9 :
            screen.setText(screen.getText() + "9");
            c = +9;
            break;

        case R.id.button0 :
            screen.setText(screen.getText() + "0");
            c = +0;
            break;

        case R.id.buttonC :
            screen.setText(screen.getText() + "");
            c = 0;
            break;

        case R.id.buttonPlus :


        case R.id.buttonEquals :



        }


    }
}


怎么做计算?

最佳答案

sum1中将onCreate设置为零。当用户点击+时,请执行以下操作:


c添加到sum1
显示sum1
c重置为零,以便可以输入新数字


当用户点击=时,请执行以下操作:


c添加到sum1
显示sum1
csum1重置为零


例如,这允许您点击4 + 5 + 6 =并查看15

10-08 17:18