如何将输入存储为android计算器的变量

时间:2014-05-18 14:17:36

标签: java android

到目前为止,我已经为计算器制作了用户界面,以及在开关盒中处理的按钮。到目前为止还不错。

我的问题是如何才能让应用程序识别输入到textview中的第一个数字,然后再按下该数字设置为num1?

我的代码如下:

public class MainActivity extends Activity {
// variables declared to hold buttons and text field.
Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnc,
        btnplus, btnd, btnpnt, btnequal;
TextView text;
int num1, num2;
String input;

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

    // reference and assign the resources for the view elements
    btn0 = (Button) findViewById(R.id.button0);
    btn1 = (Button) findViewById(R.id.button1);
    btn2 = (Button) findViewById(R.id.button2);
    btn3 = (Button) findViewById(R.id.button3);
    btn4 = (Button) findViewById(R.id.button4);
    btn5 = (Button) findViewById(R.id.button5);
    btn6 = (Button) findViewById(R.id.button6);
    btn7 = (Button) findViewById(R.id.button7);
    btn8 = (Button) findViewById(R.id.button8);
    btn9 = (Button) findViewById(R.id.button9);
    btnc = (Button) findViewById(R.id.buttonc);
    text = (TextView) findViewById(R.id.textView1);
    btnd = (Button) findViewById(R.id.button16);
    btnpnt = (Button) findViewById(R.id.buttonpnt);
    btnplus = (Button) findViewById(R.id.buttonplus);


    // add the click listeners
    btn0.setOnClickListener(onClickListener);
    btn1.setOnClickListener(onClickListener);
    btn2.setOnClickListener(onClickListener);
    btn3.setOnClickListener(onClickListener);
    btn4.setOnClickListener(onClickListener);
    btn5.setOnClickListener(onClickListener);
    btn6.setOnClickListener(onClickListener);
    btn7.setOnClickListener(onClickListener);
    btn8.setOnClickListener(onClickListener);
    btn9.setOnClickListener(onClickListener);
    btnc.setOnClickListener(onClickListener);
    btnd.setOnClickListener(onClickListener);
    btnpnt.setOnClickListener(onClickListener);
    btnplus.setOnClickListener(onClickListener);
    btnequal.setOnClickListener(onClickListener);

}

private OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.button0:

            text.setText(text.getText().toString()
                    + btn0.getText().toString());

            break;

        case R.id.button1:
            text.setText(text.getText().toString()
                    + btn1.getText().toString());
            break;

        case R.id.button2:
            text.setText(text.getText().toString()
                    + btn2.getText().toString());
            break;

        case R.id.button3:
            text.setText(text.getText().toString()
                    + btn3.getText().toString());
            break;

        case R.id.button4:
            text.setText(text.getText().toString()
                    + btn4.getText().toString());
            break;

        case R.id.button5:
            text.setText(text.getText().toString()
                    + btn5.getText().toString());
            break;

        case R.id.button6:
            text.setText(text.getText().toString()
                    + btn6.getText().toString());
            break;

        case R.id.button7:
            text.setText(text.getText().toString()
                    + btn7.getText().toString());
            break;

        case R.id.button8:
            text.setText(text.getText().toString()
                    + btn8.getText().toString());
            break;

        case R.id.button9:
            text.setText(text.getText().toString()
                    + btn9.getText().toString());
            break;

        case R.id.button16:
            String contents = text.getText().toString();

            if (contents.length() > 1) {
                text.setText(contents.substring(0, contents.length() - 1));
            } else {
                text.setText("");
            }

            break;

        case R.id.button16:
            text.setText("");
            break;

        case R.id.buttonpnt:
            String dot = ".";
            String concat = text.getText() + dot;
            text.setText(concat);
            break;

        case R.id.buttonplus:
            String operator = "+";
            String concatt = text.getText() + operator;
            text.setText(concatt);
            break;

        }
    }
};

我不明白如何告诉android在添加之前输入的第一个数字被存储为num1。从那里计算将是一块蛋糕。

任何心智敏锐的人都可以帮助我

非常感谢你的时间

1 个答案:

答案 0 :(得分:1)

当用户按下按钮时,您的TextView会保留该号码,例如用户按2 5 1然后按加号按钮。在你的案例R.id.buttonplus中,TextView内容将是你的num1。所以你可以这样做:

case R.id.buttonplus:
    int num1 = Integer.parseInt(text.getText().toString());
    text.setText(text.getText() + "+");