项目实战:实现一个简单计算器

 

  1. 界面设计

1)拖进一个大文本,整屏,设计各个数字及运算,用Table来存放。

<TableLayout

       android:layout_width=“fill_parent”

       android:layout_height=“wrap_content”>

 

        <TableRow

           android:id=“@+id/tableRow1”

           android:layout_width=“fill_parent”

           android:layout_height=“wrap_content”>

            <Button

               android:id=“@+id/btn1”

                 android:layout_width=“wrap_content”

                android:layout_height=“wrap_content”

                android:layout_weight=“1”

                android:text=“1”></Button>

              <Button

                android:id=“@+id/btn2”

                 android:layout_width=“wrap_content”

                android:layout_height=“wrap_content”

                android:layout_weight=“1”

                android:text=“2”></Button

 

2实现算法,一个数字一个操作符号,执行第二个操作符号时前面就运算,有三项就执行运算,用数组记录,创建种类类:

publicclass Types {

 

   publicstaticfinalintADD = 1;

   publicstaticfinalintSUB = 2;

   publicstaticfinalintX = 3;

   publicstaticfinalintDIV = 4;

   publicstaticfinalintNUM = 5;

  

}

3)存入数字或符号,项类

publicclass Item {

 

   public Item(double value,int type){

      this.value=value;

      this.type=type;      

     

   }

   publicdoublevalue=0;

   publicinttype=0;

  

  

}

4)定义数组,存放内容为Item

private List<Item>items = new ArrayList<Item>();

 

(5) 如果输入数字,直接添加:

publicvoid onClick(View v) {

      switch (v.getId()) {

      case R.id.btn0:

          tvScreen.append(“0”);

          break;

      case R.id.btn1:

          tvScreen.append(“1”);

          break;

      case R.id.btn2:

          tvScreen.append(“2”);

          break;

。。。。。

6)实现相加

case R.id.btnAdd:

          items.add(new Item(Double.parseDouble(tvScreen.getText().toString()),Types.NUM));

 

判断是否有三项了,写成一个方法:

          checkAndCompute();

 

实现:

publicvoid checkAndCompute(){

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

         

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

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

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

         

          items.clear();

         

          switch (opt) {

          case Types.ADD:

             items.add(new Item(a+b, Types.NUM));

             break;

          case Types.SUB:

             items.add(new Item(a-b, Types.NUM));

             break;

          case Types.X:

             items.add(new Item(a*b, Types.NUM));

             break;

          case Types.DIV:

             items.add(new Item(a/b, Types.NUM));

             break;

          }

      }

   }

 

7)结构保存,并且屏幕清空

 

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

          tvScreen.setText(“”);

          break;

 

(8)实现减等其它运算

 case R.id.btnSub:

          items.add(new Item(Double.parseDouble(tvScreen.getText().toString()),InputTypes.NUM));

          checkAndCompute();

          items.add(new Item(0, InputTypes.SUB));

          tvScreen.setText(“”);

          break;

9)实现等于号等运算

case R.id.btnResult:

          items.add(new Item(Double.parseDouble(tvScreen.getText().toString()),InputTypes.NUM));

          checkAndCompute();

          tvScreen.setText(items.get(0).value+“”);

          items.clear();

          break;