Android计算器简单逻辑实现

引言:

  我的android计算器的实现方式是:按钮输入一次,就处理一次。

  但是如果你学过数据结构(栈),就可以使用表达式解析(前缀,后缀)处理。

  而这个方式已经很成熟了,但是时间有限,只完成了这个简单的计算器。

  至于,这个Android的布局已经在我博客中发布了,不再讲述。           

  1 package com.example.androidlessontwo;  2   3 import android.os.Bundle;  4 import android.app.Activity;  5 import android.view.Menu;  6 import android.view.View;  7 import android.widget.Button;  8 import android.widget.TextView;  9  10 public class MainActivity extends Activity  { 11  12     private Button[] buttonNum=new Button[11]; 13     private Button[] buttonComand=new Button[5]; 14     private TextView input=null; 15     private TextView rl=null; 16     private Button   buttonClear=null; 17     private boolean firstFlag=true; 18     private double result=0.0; 19     private String lastCommand; 20      21     public void MyCalculator() 22     { 23         result = 0.0; 24         firstFlag=true; 25         lastCommand="="; 26     } 27     @Override 28     protected void onCreate(Bundle savedInstanceState) { 29         super.onCreate(savedInstanceState); 30         setContentView(R.layout.activity_main); 31         buttonNum[0]=(Button) findViewById(R.id.num0); 32         buttonNum[1]=(Button) findViewById(R.id.num1); 33         buttonNum[2]=(Button) findViewById(R.id.num2); 34         buttonNum[3]=(Button) findViewById(R.id.num3); 35         buttonNum[4]=(Button) findViewById(R.id.num4); 36         buttonNum[5]=(Button) findViewById(R.id.num5); 37         buttonNum[6]=(Button) findViewById(R.id.num6); 38         buttonNum[7]=(Button) findViewById(R.id.num7); 39         buttonNum[8]=(Button) findViewById(R.id.num8); 40         buttonNum[9]=(Button) findViewById(R.id.num9); 41         buttonNum[10]=(Button) findViewById(R.id.point); 42          43         buttonComand[0]=(Button) findViewById(R.id.add); 44         buttonComand[1]=(Button) findViewById(R.id.sub); 45         buttonComand[2]=(Button) findViewById(R.id.ride); 46         buttonComand[3]=(Button) findViewById(R.id.divide); 47         buttonComand[4]=(Button) findViewById(R.id.equal); 48          49         input=(TextView) findViewById(R.id.input); 50         rl   =(TextView) findViewById(R.id.rl); 51         buttonClear=(Button) findViewById(R.id.clean); 52          53         NumberAction na= new NumberAction(); 54         CommandAction ca=new CommandAction(); 55         for(Button bc:buttonComand) 56         { 57             bc.setOnClickListener(ca); 58         } 59         for(Button bc:buttonNum) 60         { 61             bc.setOnClickListener(na); 62         } 63         buttonClear.setOnClickListener(new Button.OnClickListener() 64         { 65  66             @Override 67             public void onClick(View v) { 68                 MyCalculator(); 69                 rl.setText("0.0"); 70             } 71         }); 72     } 73     @Override 74     public boolean onCreateOptionsMenu(Menu menu) { 75         // Inflate the menu; this adds items to the action bar if it is present. 76         getMenuInflater().inflate(R.menu.main, menu); 77         return true; 78     } 79     private class NumberAction implements Button.OnClickListener 80     { 81  82         @Override 83         public void onClick(View view)  84         { 85             Button btn = (Button)view; 86             String inputTemp =btn.getText().toString();//6 87             input.setText(input.getText().toString()+inputTemp);     88             double numtemp = 0; 89             switch(btn.getId()) 90             { 91                 case R.id.num0: 92                 { 93                     if(firstFlag) 94                         { 95                             result=result*10+0; 96                             firstFlag=false; 97                         } 98                     else 99                         numtemp=numtemp*10+0;100                     break;101                 }102                 case R.id.num1:103                 {104                     if(firstFlag)105                         {106                         result=result*10+1;107                             firstFlag=false;108                         }109                     else110                         numtemp=numtemp*10+1;111                     break;112                 }113                 case R.id.num2:114                 {115                     if(firstFlag)116                         {117                         result=result*10+2;118                             firstFlag=false;119                         }120                     else121                         numtemp=numtemp*10+2;122                     break;123                 }124                 case R.id.num3:125                 {126                     if(firstFlag)127                         {128                         result=result*10+3;129                             firstFlag=false;130                         }131                     else132                         numtemp=numtemp*10+3;133                     break;134                 }135                 case R.id.num4:136                 {137                     if(firstFlag)138                         {139                         result=result*10+4;140                             firstFlag=false;141                         }142                     else143                         numtemp=numtemp*10+4;144                     break;145                 }146                 case R.id.num5:147                 {148                     if(firstFlag)149                         {150                         result=result*10+5;151                             firstFlag=false;152                         }153                     else154                         numtemp=numtemp*10+5;155                     break;156                 }157                 case R.id.num6:158                 {159                     if(firstFlag)160                         {161                         result=result*10+6;162                             firstFlag=false;163                         }164                     else165                         {166                             numtemp=numtemp*10+6;167                             calculate(numtemp);168                         }169                     break;170                 }171                 case R.id.num7:172                 {173                     if(firstFlag)174                         {175                             result=result*10+7;176                             firstFlag=false;177                         }178                     else179                     {180                         numtemp=numtemp*10+7;181                         calculate(numtemp);182                     }183                     break;184                 }185                 case R.id.num8:186                 {187                     if(firstFlag)188                         {189                         result=result*10+8;190                             {191                                 result=result*10+8;192                                 firstFlag=false;193                             }194                         }195                     else196                         {197                             numtemp=numtemp*10+8;198                             calculate(numtemp);199                         }200                     break;201                 }202                 case R.id.num9:203                 {204                     if(firstFlag)205                         {206                         result=result*10+9;207                             firstFlag=false;208                         }209                     else210                         {211                             numtemp=numtemp*10+9;212                             calculate(numtemp);213                         }214                     break;215                 }    216             }            217             218             219                 220             221         }222         223     }224     225     private class CommandAction implements Button.OnClickListener226     {227         @Override228         public void onClick(View v) 229         {230             Button btn=(Button)v;231             String inputCommand=(String)btn.getText();232             switch(btn.getId())233             {234                 case R.id.add:235                 {236                     lastCommand="+";237                     break;238                 }239                 case R.id.sub:240                 {241                     lastCommand="-";242                     break;243                 }244                 case R.id.ride:245                 {246                     lastCommand="*";247                     break;248                 }249                 case R.id.divide:250                 {251                     lastCommand="/";252                     break;253                 }254                 case R.id.equal:255                 {256                     lastCommand="=";257                     input.setText("");258                     rl.setText(String.valueOf(result));259                     return ;260                 }261                     262             }263             input.setText(input.getText()+inputCommand);    264         }265         266     }267     private void calculate(double x)268     {269         270         271          if(lastCommand.equals("+"))272             {273                 result += x;274             }275             276          if(lastCommand.equals("-"))277             {278                 result -= x;279             }280         281          if(lastCommand.equals("*"))282             {283                 result *= x;284             }285         286          if(lastCommand.equals("/"))287             {288                 result /= x;289             }290     }291     292 }

复制去Google翻译翻译结果

更多相关文章

  1. Android 以webview的方式集成Dcloud h5+SDK
  2. Android中更新UI的四种常用方式
  3. Android 设置TextView字体Color Selector的正确方式
  4. Android 开发之旅:view的几种布局方式及实践
  5. 【安卓开发】UI设计基础4:用网格布局 GridLayout 实现计算器UI
  6. Android中的布局方式(二)
  7. 键盘弹出以后Activity的布局方式

随机推荐

  1. 一个Android(安卓)Service小例子
  2. Android屏幕自适应的问题
  3. android textview 特出显示
  4. 解决:Unable to connect to repository ht
  5. MAC中设置android adb环境变量
  6. java.lang.ClassCastException: android.
  7. 错误 'roundIcon' in package 'android'
  8. android BitmapFactory的OutOfMemoryErro
  9. Android 显示Emoji表情字符
  10. 【Android】Uri、UriMatcher、ContentUri