Android简单计算器

时间:2014-06-19 08:43:52

标签: java android textview calculator

我是Android应用程序开发的新手,我正在研究一个简单的计算器。 enter image description here

MainActivity.java文件如下 -

public class MainActivity extends Activity implements OnClickListener 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View add = (Button)findViewById(R.id.Add);
    add.setOnClickListener(this);
    View substract = (Button)findViewById(R.id.Substract);
    substract.setOnClickListener(this);
    View multiply = (Button)findViewById(R.id.Multiply);
    multiply.setOnClickListener(this);
    View divide = (Button)findViewById(R.id.Divide);
    divide.setOnClickListener(this);
    View equals = (Button)findViewById(R.id.Equals);
    equals.setOnClickListener(this);
    //Numbers
    View nu1 = (Button)findViewById(R.id.n1);
    nu1.setOnClickListener(this);
    View nu2 = (Button)findViewById(R.id.n2);
    nu2.setOnClickListener(this);
    View nu3 = (Button)findViewById(R.id.n3);
    nu3.setOnClickListener(this);
    View nu4 = (Button)findViewById(R.id.n4);
    nu4.setOnClickListener(this);
    View nu5 = (Button)findViewById(R.id.n5);
    nu5.setOnClickListener(this);
    View nu6 = (Button)findViewById(R.id.n6);
    nu6.setOnClickListener(this);
    View nu7 = (Button)findViewById(R.id.n7);
    nu7.setOnClickListener(this);
    View nu8 = (Button)findViewById(R.id.n8);
    nu8.setOnClickListener(this);
    View nu9 = (Button)findViewById(R.id.n9);
    nu9.setOnClickListener(this);
    View nu0 = (Button)findViewById(R.id.n0);
    nu0.setOnClickListener(this);
    View ac = (Button)findViewById(R.id.AC);
    ac.setOnClickListener(this);
    View ce = (Button)findViewById(R.id.CE);
    ce.setOnClickListener(this);
    View del = (Button)findViewById(R.id.DEL);
    del.setOnClickListener(this);
    View pnt = (Button)findViewById(R.id.np);
    pnt.setOnClickListener(this);

}

@Override
public void onClick(View v) 
{
    float num1=0,numx=0;
    int choice=0;
    TextView num = (TextView)findViewById(R.id.Number);
    num.setOnClickListener(this);
    switch (v.getId())
    {
        case R.id.Add:
            choice=1;
            numx=num1;
            num1=0;
            num.setText("");
            break;
        case R.id.Substract:
            choice=2;
            numx=num1;
            num1=0;
            num.setText("");
            break;
        case R.id.Multiply:
            choice=3;
            numx=num1;
            num1=0;
            num.setText("");
            break;
        case R.id.Divide:
            choice=4;
            numx=num1;
            num1=0;
            num.setText("");
            break;
        case R.id.Equals:
            switch(choice)
            {
                case 1:
                    num1=numx+num1;
                    num.setText(""+num1);
                    choice=0;
                    break;
                case 2:
                    num1=num1-numx;
                    num.setText(""+num1);
                    choice=0;
                    break;
                case 3:
                    num1=num1*numx;
                    num.setText(""+num1);
                    choice=0;
                    break;
                case 4:
                    num1=num1/numx;
                    num.setText(""+num1);
                    choice=0;
                    break;
                case 0:
                    num.setText(""+num1);
                    choice=0;
                    break;
            }
        case R.id.n1:
            num1=num1*10+1;
            num.setText(""+num1);
            System.out.print(num1);
            break;
        case R.id.n2:
            num1=num1*10+2;
            num.setText(""+num1);
            break;
        case R.id.n3:
            num1=num1*10+3;
            num.setText(""+num1);
            break;
        case R.id.n4:
            num1=num1*10+4;
            num.setText(""+num1);
            break;
        case R.id.n5:
            num1=num1*10+5;
            num.setText(""+num1);
            break;
        case R.id.n6:
            num1=num1*10+6;
            num.setText(""+num1);
            break;
        case R.id.n7:
            num1=num1*10+7;
            num.setText(""+num1);
            break;
        case R.id.n8:
            num1=num1*10+8;
            num.setText(""+num1);
            break;
        case R.id.n9:
            num1=num1*10+9;
            num.setText(""+num1);
            break;
        case R.id.n0:
            num1=num1*10;
            num.setText(""+num1);
            break;
    }


}

}

我面对两个问题 -

  1. 如果我想输入多于一位数的任何数字,则不会发生。就像我按53,首先textview显示5.0然后3.0

  2. 其次,按下=按钮

  3. 后,所有计算总是给出输出1.0

    任何人都可以帮助我!

2 个答案:

答案 0 :(得分:1)

为什么要在num上设置点击监听器?

您正在定义选项,并在onClick方法中单击数字,并在每次单击其中一个按钮时重新初始化它们。这些应该是活动字段的一部分

答案 1 :(得分:0)

您必须在切换案例中添加textview num 的文本(add,sub).Like num.getText().toString().