1 packagecom.example.androidlessontwo;2
3 importandroid.os.Bundle;4 importandroid.app.Activity;5 importandroid.view.Menu;6 importandroid.view.View;7 importandroid.widget.Button;8 importandroid.widget.TextView;9
10 public class MainActivity extendsActivity {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 privateString lastCommand;20
21 public voidMyCalculator()22 {23 result = 0.0;24 firstFlag=true;25 lastCommand="=";26 }27 @Override28 protected voidonCreate(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= newNumberAction();54 CommandAction ca=newCommandAction();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(newButton.OnClickListener()64 {65
66 @Override67 public voidonClick(View v) {68 MyCalculator();69 rl.setText("0.0");70 }71 });72 }73 @Override74 public booleanonCreateOptionsMenu(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 implementsButton.OnClickListener80 {81
82 @Override83 public voidonClick(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 caseR.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 caseR.id.num1:103 {104 if(firstFlag)105 {106 result=result*10+1;107 firstFlag=false;108 }109 else
110 numtemp=numtemp*10+1;111 break;112 }113 caseR.id.num2:114 {115 if(firstFlag)116 {117 result=result*10+2;118 firstFlag=false;119 }120 else
121 numtemp=numtemp*10+2;122 break;123 }124 caseR.id.num3:125 {126 if(firstFlag)127 {128 result=result*10+3;129 firstFlag=false;130 }131 else
132 numtemp=numtemp*10+3;133 break;134 }135 caseR.id.num4:136 {137 if(firstFlag)138 {139 result=result*10+4;140 firstFlag=false;141 }142 else
143 numtemp=numtemp*10+4;144 break;145 }146 caseR.id.num5:147 {148 if(firstFlag)149 {150 result=result*10+5;151 firstFlag=false;152 }153 else
154 numtemp=numtemp*10+5;155 break;156 }157 caseR.id.num6:158 {159 if(firstFlag)160 {161 result=result*10+6;162 firstFlag=false;163 }164 else
165 {166 numtemp=numtemp*10+6;167 calculate(numtemp);168 }169 break;170 }171 caseR.id.num7:172 {173 if(firstFlag)174 {175 result=result*10+7;176 firstFlag=false;177 }178 else
179 {180 numtemp=numtemp*10+7;181 calculate(numtemp);182 }183 break;184 }185 caseR.id.num8:186 {187 if(firstFlag)188 {189 result=result*10+8;190 {191 result=result*10+8;192 firstFlag=false;193 }194 }195 else
196 {197 numtemp=numtemp*10+8;198 calculate(numtemp);199 }200 break;201 }202 caseR.id.num9:203 {204 if(firstFlag)205 {206 result=result*10+9;207 firstFlag=false;208 }209 else
210 {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 implementsButton.OnClickListener226 {227 @Override228 public voidonClick(View v)229 {230 Button btn=(Button)v;231 String inputCommand=(String)btn.getText();232 switch(btn.getId())233 {234 caseR.id.add:235 {236 lastCommand="+";237 break;238 }239 caseR.id.sub:240 {241 lastCommand="-";242 break;243 }244 caseR.id.ride:245 {246 lastCommand="*";247 break;248 }249 caseR.id.divide:250 {251 lastCommand="/";252 break;253 }254 caseR.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(doublex)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 }