如何制作简单的计算器在android上的EditText中返回最终结果

时间:2012-06-08 10:17:59

标签: java android calculator

我现在处于java和eclipse中,我只是想尝试构建一个简单的计算器,但我不能让它返回结果。

我的计划基本上是当用户单击其中一个运算符并且EditText不为空时,First变量将与EditText相等,并且Operator变量将更改,并且当用户单击Result按钮时, First变量不为空,则EditText将与Second变量相等,Result变量将基本等于结果。我认为我搞砸了类型和变量,但我无法弄明白究竟是什么。

有人可以帮忙吗?

这是java代码;

public class Main extends Activity implements OnClickListener {
LinearLayout linear;

float First, Second, Operator, Result;

Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bResult, bTizedes, bSzorzas,  bKivonas, bOsztas, bOsszeadas;
EditText eT;

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

    First = 0;
    Second = 0;
    Operator = 0;
    Result = 0;

    b0 = (Button) findViewById(R.id.b0);
    b1 = (Button) findViewById(R.id.b1);
    b2 = (Button) findViewById(R.id.b2);
    b3 = (Button) findViewById(R.id.b3);
    b4 = (Button) findViewById(R.id.b4);
    b5 = (Button) findViewById(R.id.b5);
    b6 = (Button) findViewById(R.id.b6);
    b7 = (Button) findViewById(R.id.b7);
    b8 = (Button) findViewById(R.id.b8);
    b9 = (Button) findViewById(R.id.b9);
    bTizedes = (Button) findViewById(R.id.bTizedes);
    bSzorzas = (Button) findViewById(R.id.bSzorzas);
    bResult = (Button) findViewById(R.id.bEgyenlo);
    bKivonas = (Button) findViewById(R.id.bKivonas);
    bOsztas = (Button) findViewById(R.id.bOsztas);
    bOsszeadas = (Button) findViewById(R.id.bOsszeadas);
    eT = (EditText) findViewById(R.id.eT);

    b0.setOnClickListener(this);b1.setOnClickListener(this);b2.setOnClickListener(this);b3.setOnClickListener(this);
    b4.setOnClickListener(this);b5.setOnClickListener(this);b6.setOnClickListener(this);b7.setOnClickListener(this);
    b8.setOnClickListener(this);b9.setOnClickListener(this);bTizedes.setOnClickListener(this);bSzorzas.setOnClickListener(this);
    bResult.setOnClickListener(this);bKivonas.setOnClickListener(this);bOsztas.setOnClickListener(this);bOsszeadas.setOnClickListener(this);

    bSzorzas.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
            if(First == 0)
        {
            EditText eT = (EditText) findViewById(R.id.eT);
            float First = Float.valueOf(eT.getText().toString());
            Operator = 2;
            eT.setText(null);
        }
        else if(First != 0)
        {
            Operator = 2;
        };
        }
    });
    bKivonas.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
                if(First == 0)
            {
                EditText eT = (EditText) findViewById(R.id.eT);
                float First = Float.valueOf(eT.getText().toString());
                Operator = 4;
                eT.setText(null);
            }
            else if(First != 0)
            {
                Operator = 4;
            };
            }
        });
    bOsztas.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
                if(First == 0)
            {
                EditText eT = (EditText) findViewById(R.id.eT);
                float First = Float.valueOf(eT.getText().toString());
                Operator = 1;
                eT.setText(null);
            }
            else if(First != 0)
            {
                Operator = 1;
            };
            }
        });
    bOsszeadas.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
                if(First == 0)
            {
                EditText eT = (EditText) findViewById(R.id.eT);
                float First = Float.valueOf(eT.getText().toString());
                Operator = 3;
                eT.setText(null);
            }
            else if(First != 0)
            {
                Operator = 3;
            };
            }
        });
    bResult.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
                if(First != 0)
            {
                EditText eT = (EditText) findViewById(R.id.eT);
                float Second = Float.valueOf(eT.getText().toString());

                if(Operator == 1){
                    int Result = (int) (First) / (int) (Second);
                    eT.setText(Result);
                }

                else if(Operator == 2){
                    int Result = (int) (First) * (int) (Second);
                    eT.setText(Result);
                }

                else if(Operator == 3){
                    int Result = (int) (First) + (int) (Second);
                    eT.setText(Result);
                }

                else if(Operator == 4){
                    int Result = (int) (First) - (int) (Second);
                    eT.setText(Result);
                }

                eT.setText(null);
            }
            else if(First == 0)
            {

            };
            }
        });

}
    public void onClick(View v) {

        switch(v.getId()){

        case R.id.b0:
            eT.setText( eT.getText() + "0");
            break;
        case R.id.b1:
            eT.setText( eT.getText() + "1");
            break;
        case R.id.b2:
            eT.setText( eT.getText() + "2");
            break;
        case R.id.b3:
            eT.setText( eT.getText() + "3");
            break;
        case R.id.b4:
            eT.setText( eT.getText() + "4");
            break;
        case R.id.b5:
            eT.setText( eT.getText() + "5");
            break;
        case R.id.b6:
            eT.setText( eT.getText() + "6");
            break;
        case R.id.b7:
            eT.setText( eT.getText() + "7");
            break;
        case R.id.b8:
            eT.setText( eT.getText() + "8");
            break;
        case R.id.b9:
            eT.setText( eT.getText() + "9");
            break;
    } 
    }}

2 个答案:

答案 0 :(得分:1)

尝试专注于此Tutorial,看看事情是如何完成的,并尝试改进它

答案 1 :(得分:0)

 float First = Float.valueOf(eT.getText().toString());

2个问题:

  1. 为什么要在这里重新声明变量First?只需使用它。
  2. 要解析,请使用Float.parseFloat(eT.getText().toString());
  3. 为什么要将变量命名为First?使用first。遵循惯例。

    编辑:

    可以看到重新声明几乎所有变量的相同问题。请不要这样做。