第一个安卓程序:计算器

效果大致如下:

获取layout中的控件并添加事件:(利用TableLayout)

tvResult= (TextView) findViewById(R.id.tvResult);

findViewById(R.id.btn0).setOnClickListener(this);

findViewById(R.id.btn1).setOnClickListener(this);

findViewById(R.id.btn2).setOnClickListener(this);

findViewById(R.id.btn3).setOnClickListener(this);

findViewById(R.id.btn4).setOnClickListener(this);

findViewById(R.id.btn5).setOnClickListener(this);

findViewById(R.id.btn6).setOnClickListener(this);

findViewById(R.id.btn7).setOnClickListener(this);

findViewById(R.id.btn8).setOnClickListener(this);

findViewById(R.id.btn9).setOnClickListener(this);

findViewById(R.id.btnAdd).setOnClickListener(this);

findViewById(R.id.btnMinus).setOnClickListener(this);

findViewById(R.id.btnMul).setOnClickListener(this);

findViewById(R.id.btnDiv).setOnClickListener(this);

findViewById(R.id.btnResult).setOnClickListener(this);

findViewById(R.id.btnClear).setOnClickListener(this);


创建一个数组用来存放需要 运算的数字和运算符 

private Listitems = new ArrayList();

Item用来存储数据和类型type(类型type:加、减、乘、除或结果)

public class Item {

       public int type=0;

       public double value=0;

       public Item(double value,int type) {

                  this.type= type;

                  this.value= value;

      }

}


声明Types存放type

public class Types {

      public static final intADD=1;

      public static final intMINUS=2;

      public static final intMUL=3;

      public static final intDIV=4;

      public static final intRESULT=5;

}


实现点击事件的处理:

public void onClick(View v) {

           switch(v.getId()) {

                     case R.id.btn0:

                                 tvResult.append("0");

                     break;

                         .

                         .

                         .

                     case R.id.btn9:

                                 tvResult.append("9");

                     break;

                     case R.id.btnAdd:

                               items.add(new Item(Double.parseDouble(tvResult.getText().toString()), Types.RESULT));

                              checkAndCalculat();

                              items.add(new Item(0, Types.ADD));

                              tvResult.setText("");

                   break;

                       .

                       .

                       .

                   case R.id.btnClear:

                          tvResult.setText("");

                          items.clear();

                   break;

                   case R.id.btnResult:

                            items.add(new Item(Double.parseDouble(tvResult.getText().toString()), Types.RESULT));

                           checkAndCalculat();

                           tvResult.setText(items.get(0).value+"");

                           items.clear();

                  break;

checkAndCalculat代码的简单封装。核对并且运算

private void checkAndCalculat() {

          if(items.size()>=3) {

                  double a =items.get(0).value;

                  double b =items.get(2).value;

                   int type =items.get(1).type;

                   items.clear();

                   switch(type) {

                           caseTypes.ADD:

                                    items.add(newItem(a+b, Types.ADD));

                           break;

                           caseTypes.MINUS:

                                    items.add(newItem(a-b, Types.MINUS));

                           break;

                           caseTypes.MUL:

                                     items.add(newItem(a*b, Types.MUL));

                           break;

                           caseTypes.DIV:

                                    items.add(newItem(a/b, Types.DIV));

                           break;

                   }

         }

}

你可能感兴趣的:(第一个安卓程序:计算器)