安卓自制的计算器
这是本人自己用安卓编写的计算器,代码的优化可能有所问题,不过大家都在学习阶段,欢迎大家批评指正,还是上源码吧。
先上一波运行图
下面是XML文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context="com.example.nanchen.exam_1_1.MainActivity"> 9 10 11 12 13 <!--显示结果--> 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="0dp" 17 android:layout_weight="1"> 18 <TextView 19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:id="@+id/result" 22 android:gravity="end" 23 android:textSize="40sp" 24 android:text="@string/zero"/> 25 </LinearLayout> 26 27 <LinearLayout 28 android:layout_width="match_parent" 29 android:layout_height="0dp" 30 android:orientation="horizontal" 31 android:layout_weight="1"> 32 33 <Button 34 android:layout_width="0dp" 35 android:layout_weight="1" 36 android:layout_height="wrap_content" 37 android:text="@string/clear" 38 android:id="@+id/clear" 39 android:textAllCaps="false" 40 android:gravity="center" 41 android:textSize="25sp"/> 42 43 <Button 44 android:layout_width="0dp" 45 android:layout_weight="1" 46 android:layout_height="wrap_content" 47 android:text="@string/backspace" 48 android:id="@+id/backSpace" 49 android:textAllCaps="false" 50 android:gravity="center" 51 android:textSize="25sp" 52 /> 53 </LinearLayout> 54 55 <!--789/--> 56 <LinearLayout 57 android:layout_width="match_parent" 58 android:layout_height="0dp" 59 android:layout_weight="1"> 60 <Button 61 android:layout_width="0dp" 62 android:text="@string/seven" 63 android:id="@+id/btn7" 64 android:textSize="30sp" 65 android:gravity="center" 66 android:layout_height="wrap_content" 67 android:layout_weight="1"/> 68 <Button 69 android:layout_width="0dp" 70 android:text="@string/eight" 71 android:id="@+id/btn8" 72 android:textSize="30sp" 73 android:gravity="center" 74 android:layout_height="wrap_content" 75 android:layout_weight="1"/> 76 <Button 77 android:layout_width="0dp" 78 android:text="@string/nine" 79 android:id="@+id/btn9" 80 android:textSize="30sp" 81 android:gravity="center" 82 android:layout_height="wrap_content" 83 android:layout_weight="1"/> 84 <Button 85 android:layout_width="0dp" 86 android:text="@string/del" 87 android:id="@+id/btn13" 88 android:textSize="30sp" 89 android:gravity="center" 90 android:layout_height="wrap_content" 91 android:layout_weight="1"/> 92 93 </LinearLayout> 94 95 <!--456*--> 96 <LinearLayout 97 android:layout_width="match_parent" 98 android:layout_height="0dp" 99 android:layout_weight="1"> 100 <Button 101 android:layout_width="0dp" 102 android:text="@string/four" 103 android:id="@+id/btn4" 104 android:textSize="30sp" 105 android:gravity="center" 106 android:layout_height="wrap_content" 107 android:layout_weight="1"/> 108 <Button 109 android:layout_width="0dp" 110 android:text="@string/five" 111 android:id="@+id/btn5" 112 android:textSize="30sp" 113 android:gravity="center" 114 android:layout_height="wrap_content" 115 android:layout_weight="1"/> 116 <Button 117 android:layout_width="0dp" 118 android:text="@string/six" 119 android:id="@+id/btn6" 120 android:textSize="30sp" 121 android:gravity="center" 122 android:layout_height="wrap_content" 123 android:layout_weight="1"/> 124 <Button 125 android:layout_width="0dp" 126 android:text="@string/mul" 127 android:id="@+id/btn12" 128 android:textSize="30sp" 129 android:gravity="center" 130 android:layout_height="wrap_content" 131 android:layout_weight="1"/> 132 133 </LinearLayout> 134 135 <!--123- --> 136 <LinearLayout 137 android:layout_width="match_parent" 138 android:layout_height="0dp" 139 android:layout_weight="1"> 140 141 <Button 142 android:layout_width="0dp" 143 android:text="@string/one" 144 android:textSize="30sp" 145 android:id="@+id/btn1" 146 android:gravity="center" 147 android:layout_height="wrap_content" 148 android:layout_weight="1"/> 149 <Button 150 android:layout_width="0dp" 151 android:text="@string/two" 152 android:id="@+id/btn2" 153 android:textSize="30sp" 154 android:gravity="center" 155 android:layout_height="wrap_content" 156 android:layout_weight="1"/> 157 <Button 158 android:layout_width="0dp" 159 android:text="@string/three" 160 android:id="@+id/btn3" 161 android:textSize="30sp" 162 android:gravity="center" 163 android:layout_height="wrap_content" 164 android:layout_weight="1"/> 165 <Button 166 android:layout_width="0dp" 167 android:text="@string/sub" 168 android:id="@+id/btn11" 169 android:textSize="30sp" 170 android:gravity="center" 171 android:layout_height="wrap_content" 172 android:layout_weight="1"/> 173 174 </LinearLayout> 175 176 <!--0.=+--> 177 <LinearLayout 178 android:layout_width="match_parent" 179 android:layout_height="0dp" 180 android:layout_weight="1"> 181 182 <Button 183 android:layout_width="0dp" 184 android:text="@string/zero1" 185 android:id="@+id/btn0" 186 android:textSize="30sp" 187 android:gravity="center" 188 android:layout_height="wrap_content" 189 android:layout_weight="1"/> 190 <Button 191 android:layout_width="0dp" 192 android:text="@string/dec" 193 android:id="@+id/btn14" 194 android:textSize="30sp" 195 android:gravity="center" 196 android:layout_height="wrap_content" 197 android:layout_weight="1"/> 198 <Button 199 android:layout_width="0dp" 200 android:text="@string/equal" 201 android:id="@+id/btn15" 202 android:textSize="30sp" 203 android:gravity="center" 204 android:layout_height="wrap_content" 205 android:layout_weight="1"/> 206 <Button 207 android:layout_width="0dp" 208 android:text="@string/add" 209 android:id="@+id/btn10" 210 android:textSize="30sp" 211 android:gravity="center" 212 android:layout_height="wrap_content" 213 android:layout_weight="1"/> 214 215 </LinearLayout> 216 217 218 219 <LinearLayout 220 android:layout_weight="4" 221 android:orientation="vertical" 222 android:layout_width="match_parent" 223 android:layout_height="0dp"> 224 225 <TextView 226 android:layout_width="wrap_content" 227 android:layout_height="wrap_content" 228 android:textSize="20sp" 229 android:text="@string/string1"/> 230 <TextView 231 android:layout_width="wrap_content" 232 android:layout_height="wrap_content" 233 android:text="@string/string2" 234 android:textSize="20sp"/> 235 236 </LinearLayout> 237 238 239 240 </LinearLayout>
下面上活动的代码
1 package com.example.nanchen.exam_1_1; 2 3 import android.content.DialogInterface; 4 import android.support.v7.app.AlertDialog; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.TextView; 10 11 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 12 private Button btn0; 13 private Button btn1; 14 private Button btn2; 15 private Button btn3; 16 private Button btn4; 17 private Button btn5; 18 private Button btn6; 19 private Button btn7; 20 private Button btn8; 21 private Button btn9; 22 private Button btnAdd; 23 private Button btnSub; 24 private Button btnMul; 25 private Button btnDiv; 26 private Button btnDec; 27 private Button btnEqual; 28 private Button btnClear; 29 private Button btnBackSpace; 30 private TextView textView_result; 31 32 double num1 = 0,num2 = 0,result = 0;//定义存储结果和两个操作数 33 boolean isCheckEqu = false;//检测是否点击了等于 34 int op = 0;//定义运算符 35 boolean hasDec = false; 36 boolean hasOp = false; 37 38 39 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.activity_main); 44 45 textView_result = (TextView) findViewById(R.id.result); 46 btn0 = (Button) findViewById(R.id.btn0); 47 btn1 = (Button) findViewById(R.id.btn1); 48 btn2 = (Button) findViewById(R.id.btn2); 49 btn3 = (Button) findViewById(R.id.btn3); 50 btn4 = (Button) findViewById(R.id.btn4); 51 btn5 = (Button) findViewById(R.id.btn5); 52 btn6 = (Button) findViewById(R.id.btn6); 53 btn7 = (Button) findViewById(R.id.btn7); 54 btn8 = (Button) findViewById(R.id.btn8); 55 btn9 = (Button) findViewById(R.id.btn9); 56 btnAdd = (Button) findViewById(R.id.btn10); 57 btnSub = (Button) findViewById(R.id.btn11); 58 btnMul = (Button) findViewById(R.id.btn12); 59 btnDiv = (Button) findViewById(R.id.btn13); 60 btnDec = (Button) findViewById(R.id.btn14); 61 btnEqual = (Button) findViewById(R.id.btn15); 62 btnClear = (Button) findViewById(R.id.clear); 63 btnBackSpace = (Button) findViewById(R.id.backSpace); 64 65 btn0.setOnClickListener(this); 66 btn1.setOnClickListener(this); 67 btn2.setOnClickListener(this); 68 btn3.setOnClickListener(this); 69 btn4.setOnClickListener(this); 70 btn5.setOnClickListener(this); 71 btn6.setOnClickListener(this); 72 btn7.setOnClickListener(this); 73 btn8.setOnClickListener(this); 74 btn9.setOnClickListener(this); 75 btnAdd.setOnClickListener(this); 76 btnSub.setOnClickListener(this); 77 btnDec.setOnClickListener(this); 78 btnDiv.setOnClickListener(this); 79 btnEqual.setOnClickListener(this); 80 btnMul.setOnClickListener(this); 81 btnClear.setOnClickListener(this); 82 btnBackSpace.setOnClickListener(this); 83 84 85 86 } 87 88 @Override 89 public void onClick(View v) { 90 switch (v.getId()){ 91 case R.id.btn0: 92 if(isCheckEqu){ 93 textView_result.setText(null); 94 isCheckEqu=false; 95 } 96 String myString0 = textView_result.getText().toString(); 97 if (myString0.equals("0")){ 98 myString0 = ""; 99 } 100 myString0 += "0"; 101 textView_result.setText(myString0); 102 break; 103 case R.id.btn1: 104 if(isCheckEqu){ 105 textView_result.setText(null); 106 isCheckEqu=false; 107 } 108 String myString1 = textView_result.getText().toString(); 109 if (myString1.equals("0")){ 110 myString1 = ""; 111 } 112 myString1 += "1"; 113 textView_result.setText(myString1); 114 break; 115 case R.id.btn2: 116 if(isCheckEqu){ 117 textView_result.setText(null); 118 isCheckEqu=false; 119 } 120 String myString2 = textView_result.getText().toString(); 121 if (myString2.equals("0")){ 122 myString2 = ""; 123 } 124 myString2 += "2"; 125 textView_result.setText(myString2); 126 break; 127 case R.id.btn3: 128 if(isCheckEqu){ 129 textView_result.setText(null); 130 isCheckEqu=false; 131 } 132 String myString3 = textView_result.getText().toString(); 133 if (myString3.equals("0")){ 134 myString3 = ""; 135 } 136 myString3 += "3"; 137 textView_result.setText(myString3); 138 break; 139 case R.id.btn4: 140 if(isCheckEqu){ 141 textView_result.setText(null); 142 isCheckEqu=false; 143 } 144 String myString4 = textView_result.getText().toString(); 145 if (myString4.equals("0")){ 146 myString4 = ""; 147 } 148 myString4 += "4"; 149 textView_result.setText(myString4); 150 break; 151 case R.id.btn5: 152 if(isCheckEqu){ 153 textView_result.setText(null); 154 isCheckEqu=false; 155 } 156 String myString5 = textView_result.getText().toString(); 157 if (myString5.equals("0")){ 158 myString5 = ""; 159 } 160 myString5 += "5"; 161 textView_result.setText(myString5); 162 break; 163 case R.id.btn6: 164 if(isCheckEqu){ 165 textView_result.setText(null); 166 isCheckEqu=false; 167 } 168 String myString6 = textView_result.getText().toString(); 169 if (myString6.equals("0")){ 170 myString6 = ""; 171 } 172 myString6 += "6"; 173 textView_result.setText(myString6); 174 break; 175 case R.id.btn7: 176 if(isCheckEqu){ 177 textView_result.setText(null); 178 isCheckEqu=false; 179 } 180 String myString7 = textView_result.getText().toString(); 181 if (myString7.equals("0")){ 182 myString7 = ""; 183 } 184 myString7 += "7"; 185 textView_result.setText(myString7); 186 break; 187 case R.id.btn8: 188 if(isCheckEqu){ 189 textView_result.setText(null); 190 isCheckEqu=false; 191 } 192 String myString8 = textView_result.getText().toString(); 193 if (myString8.equals("0")){ 194 myString8 = ""; 195 } 196 myString8 += "8"; 197 textView_result.setText(myString8); 198 break; 199 case R.id.btn9: 200 if(isCheckEqu){ 201 textView_result.setText(null); 202 isCheckEqu=false; 203 } 204 String myString9 = textView_result.getText().toString(); 205 if (myString9.equals("0")){ 206 myString9 = ""; 207 } 208 myString9 += "9"; 209 textView_result.setText(myString9); 210 break; 211 212 case R.id.btn10: 213 if (hasOp){ 214 doEqual();//显示出来当前的答案 215 } 216 String myStringAdd = textView_result.getText().toString(); 217 if(myStringAdd.equals(null)){ 218 return; 219 } 220 num1 = Double.valueOf(myStringAdd); 221 textView_result.setText(null);// 这里被清除了 222 op = 1; 223 isCheckEqu = false; 224 hasDec = false; 225 hasOp = true; 226 break; 227 case R.id.btn11: 228 if (hasOp){ 229 doEqual(); 230 } 231 String myStringSub = textView_result.getText().toString(); 232 if(myStringSub.equals(null)){ 233 return; 234 } 235 num1 = Double.valueOf(myStringSub); 236 textView_result.setText(null); 237 op = 2; 238 isCheckEqu = false; 239 hasDec = false; 240 hasOp = true; 241 break; 242 case R.id.btn12: 243 if (hasOp){ 244 doEqual(); 245 } 246 String myStringMul = textView_result.getText().toString(); 247 if(myStringMul.equals(null)){ 248 return; 249 } 250 num1 = Double.valueOf(myStringMul); 251 textView_result.setText(null); 252 op = 3; 253 isCheckEqu = false; 254 hasDec = false; 255 hasOp = true; 256 break; 257 case R.id.btn13: 258 if (hasOp){ 259 doEqual(); 260 } 261 String myStringDiv = textView_result.getText().toString(); 262 if(myStringDiv.equals(null)){ 263 return; 264 } 265 num1 = Double.valueOf(myStringDiv); 266 textView_result.setText(null); 267 op = 4; 268 hasDec = false; 269 isCheckEqu = false; 270 hasOp = true; 271 break; 272 273 case R.id.btn14: 274 if(isCheckEqu){ 275 textView_result.setText(null); 276 isCheckEqu=false; 277 } 278 if(hasDec){//如果已经有小数点了,则弹出一个对话框 279 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 280 builder.setTitle("警告"); 281 builder.setMessage("输入错误,一个数字不能有两个小数点!"); 282 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 283 @Override 284 public void onClick(DialogInterface dialog, int which) { 285 286 } 287 }); 288 builder.show(); 289 return; 290 } 291 hasDec = true; 292 String myString14 = textView_result.getText().toString(); 293 myString14 += "."; 294 textView_result.setText(myString14); 295 break; 296 case R.id.btn15: 297 doEqual(); 298 hasOp = false; 299 break; 300 case R.id.backSpace: 301 String myString = textView_result.getText().toString(); 302 try { 303 textView_result.setText(myString.substring(0,myString.length()-1)); 304 }catch (Exception e){ 305 textView_result.setText("0"); 306 } 307 break; 308 case R.id.clear: 309 textView_result.setText("0"); 310 break; 311 default: 312 break; 313 314 } 315 } 316 317 /** 318 * 按下等于 319 */ 320 private void doEqual(){ 321 String myStringEqu=textView_result.getText().toString(); 322 if(myStringEqu.equals(null)) 323 { 324 return; 325 } 326 num2=Double.valueOf(myStringEqu); 327 textView_result.setText(null); 328 switch (op) { 329 case 0: 330 result=num2; 331 break; 332 case 1: 333 result=num1+num2; 334 break; 335 case 2: 336 result=num1-num2; 337 break; 338 case 3: 339 result=num1*num2; 340 break; 341 case 4: 342 result=num1/num2; 343 break; 344 default: 345 result=0; 346 break; 347 } 348 textView_result.setText(String.valueOf(result)); 349 isCheckEqu=true; 350 } 351 }
作 者:
南 尘
出 处: http://www.cnblogs.com/liushilin/
关于作者:专注于移动前端的项目开发。如有问题或建议,请多多赐教!欢迎加入Android交流群:118116509
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章下部【推荐】或侧边【关注】。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎关注我的公众号,精讲面试、算法、Andrid、Java、Python,旨在打造全网最比心的公众号。
![微信公众号 微信公众号](https://images2018.cnblogs.com/blog/845964/201807/845964-20180710101046271-1762311324.jpg)