layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:rowCount="6" 6 android:columnCount="4" 7 xmlns:tools="http://schemas.android.com/tools" 8 tools:context="com.hanqi.testapp2.PractiseActivity5"> 9 <TextView 10 android:layout_width="match_parent" 11 android:layout_height="200dp" 12 android:layout_columnSpan="4" 13 android:gravity="bottom|right" 14 android:id="@+id/tv_1" 15 android:textSize="50sp" 16 android:paddingRight="10dp" 17 android:textColor="#0F0"/> 18 <Button 19 android:layout_width="0dp" 20 android:layout_height="0dp" 21 android:text="c" 22 android:layout_columnWeight="1" 23 android:layout_rowWeight="1" 24 android:textSize="25sp" 25 android:id="@+id/bt_c"/> 26 <Button 27 android:layout_width="0dp" 28 android:layout_height="0dp" 29 android:text="←" 30 android:layout_columnWeight="1" 31 android:layout_rowWeight="1" 32 android:textSize="25sp" 33 android:id="@+id/bt_tui"/> 34 <Button 35 android:layout_width="0dp" 36 android:layout_height="0dp" 37 android:text="÷" 38 android:layout_columnWeight="1" 39 android:layout_rowWeight="1" 40 android:textSize="25sp" 41 android:id="@+id/bt_chu" 42 android:onClick="bt_chu_onClick"/> 43 <Button 44 android:layout_width="0dp" 45 android:layout_height="0dp" 46 android:text="×" 47 android:layout_columnWeight="1" 48 android:layout_rowWeight="1" 49 android:textSize="25sp" 50 android:id="@+id/bt_cheng" 51 android:onClick="bt_cheng_onClick"/> 52 <Button 53 android:layout_width="0dp" 54 android:layout_height="0dp" 55 android:text="7" 56 android:layout_columnWeight="1" 57 android:layout_rowWeight="1" 58 android:textSize="25sp" 59 android:onClick="bt_onClick" 60 android:id="@+id/bt7"/> 61 <Button 62 android:layout_width="0dp" 63 android:layout_height="0dp" 64 android:text="8" 65 android:layout_columnWeight="1" 66 android:layout_rowWeight="1" 67 android:textSize="25sp" 68 android:onClick="bt_onClick" 69 android:id="@+id/bt8"/> 70 <Button 71 android:layout_width="0dp" 72 android:layout_height="0dp" 73 android:text="9" 74 android:layout_columnWeight="1" 75 android:layout_rowWeight="1" 76 android:textSize="25sp" 77 android:onClick="bt9_onClick" 78 android:id="@+id/bt9"/> 79 <Button 80 android:layout_width="0dp" 81 android:layout_height="0dp" 82 android:text="-" 83 android:layout_columnWeight="1" 84 android:layout_rowWeight="1" 85 android:textSize="25sp" 86 android:id="@+id/bt_jian" 87 android:onClick="bt_jian_onClick"/> 88 <Button 89 android:layout_width="0dp" 90 android:layout_height="0dp" 91 android:text="4" 92 android:layout_columnWeight="1" 93 android:layout_rowWeight="1" 94 android:textSize="25sp" 95 android:id="@+id/bt4"/> 96 <Button 97 android:layout_width="0dp" 98 android:layout_height="0dp" 99 android:text="5" 100 android:layout_columnWeight="1" 101 android:layout_rowWeight="1" 102 android:textSize="25sp" 103 android:id="@+id/bt5"/> 104 <Button 105 android:layout_width="0dp" 106 android:layout_height="0dp" 107 android:text="6" 108 android:layout_columnWeight="1" 109 android:layout_rowWeight="1" 110 android:textSize="25sp" 111 android:id="@+id/bt6"/> 112 <Button 113 android:layout_width="0dp" 114 android:layout_height="0dp" 115 android:text="+" 116 android:layout_columnWeight="1" 117 android:layout_rowWeight="1" 118 android:textSize="25sp" 119 android:id="@+id/bt_jia" 120 android:onClick="bt_jia_onClick"/> 121 <Button 122 android:layout_width="0dp" 123 android:layout_height="0dp" 124 android:text="1" 125 android:layout_columnWeight="1" 126 android:layout_rowWeight="1" 127 android:textSize="25sp" 128 android:id="@+id/bt1"/> 129 <Button 130 android:layout_width="0dp" 131 android:layout_height="0dp" 132 android:text="2" 133 android:layout_columnWeight="1" 134 android:layout_rowWeight="1" 135 android:textSize="25sp" 136 android:id="@+id/bt2"/> 137 <Button 138 android:layout_width="0dp" 139 android:layout_height="0dp" 140 android:text="3" 141 android:layout_columnWeight="1" 142 android:layout_rowWeight="1" 143 android:textSize="25sp" 144 android:id="@+id/bt3"/> 145 <Button 146 android:layout_width="0dp" 147 android:layout_height="0dp" 148 android:text="=" 149 android:layout_columnWeight="1" 150 android:layout_rowSpan="2" 151 android:layout_rowWeight="1" 152 android:textSize="25sp" 153 android:layout_gravity="fill" 154 android:id="@+id/bt_deng"/> 155 <Button 156 android:layout_width="0dp" 157 android:layout_height="0dp" 158 android:text="0" 159 android:layout_columnWeight="1" 160 android:layout_columnSpan="2" 161 android:layout_rowWeight="1" 162 android:textSize="25sp" 163 android:id="@+id/bt0"/> 164 <Button 165 android:layout_width="0dp" 166 android:layout_height="0dp" 167 android:text="." 168 android:layout_columnWeight="1" 169 android:layout_rowWeight="1" 170 android:textSize="25sp" 171 android:id="@+id/bt_dian"/> 172 </GridLayout>
java类:
1 package com.hanqi.testapp2; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 9 public class PractiseActivity5 extends AppCompatActivity { 10 11 TextView tv_1; 12 Button bt9; 13 Button bt4; 14 Button bt5; 15 Button bt6; 16 Button bt_jia; 17 Button bt_jian; 18 Button bt1; 19 Button bt2; 20 Button bt3; 21 Button bt_cheng; 22 Button bt0; 23 Button bt_dian; 24 Button bt_chu; 25 Button bt_tui; 26 Button bt7; 27 Button bt8; 28 Button bt_deng; 29 Button bt_c; 30 31 //s1存储数据 32 double s1 = 0; 33 //判断运算 34 int s2 = 0; 35 //承载连续运算数字 36 double s3 = 0; 37 //显示结果 38 double s4; 39 //运算完成后使用结果直接下次运算 40 int s5=0; 41 //确认小数点输入一次 42 int s6=0; 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_practise5); 48 tv_1 = (TextView)findViewById(R.id.tv_1); 49 bt0 = (Button)findViewById(R.id.bt0); 50 bt1 = (Button)findViewById(R.id.bt1); 51 bt2 = (Button)findViewById(R.id.bt2); 52 bt3 = (Button)findViewById(R.id.bt3); 53 bt4 = (Button)findViewById(R.id.bt4); 54 bt5 = (Button)findViewById(R.id.bt5); 55 bt6 = (Button)findViewById(R.id.bt6); 56 bt7 = (Button)findViewById(R.id.bt7); 57 bt8 = (Button)findViewById(R.id.bt8); 58 bt9 = (Button)findViewById(R.id.bt9); 59 bt_jia = (Button)findViewById(R.id.bt_jia); 60 bt_jian = (Button)findViewById(R.id.bt_jian); 61 bt_cheng = (Button)findViewById(R.id.bt_cheng); 62 bt_dian = (Button)findViewById(R.id.bt_dian); 63 bt_chu = (Button)findViewById(R.id.bt_chu); 64 bt_tui = (Button)findViewById(R.id.bt_tui); 65 bt_deng = (Button)findViewById(R.id.bt_deng); 66 bt_c = (Button)findViewById(R.id.bt_c); 67 bt_tui = (Button)findViewById(R.id.bt_tui); 68 69 bt_onClick bt_l = new bt_onClick(); 70 bt_dian_onClick bt_dian1 = new bt_dian_onClick(); 71 bt_jia_onClick bt_jia1 = new bt_jia_onClick(); 72 bt_jian_onClick bt_jian1 = new bt_jian_onClick(); 73 bt_cheng_onClick bt_cheng1 = new bt_cheng_onClick(); 74 bt_chu_onClick bt_chu1 = new bt_chu_onClick(); 75 bt_deng_onClick bt_deng1 = new bt_deng_onClick(); 76 bt_c_onClick bt_c1 = new bt_c_onClick(); 77 bt_tui_onClick bt_tui1 = new bt_tui_onClick(); 78 79 bt0.setOnClickListener(bt_l); 80 bt1.setOnClickListener(bt_l); 81 bt2.setOnClickListener(bt_l); 82 bt3.setOnClickListener(bt_l); 83 bt4.setOnClickListener(bt_l); 84 bt5.setOnClickListener(bt_l); 85 bt6.setOnClickListener(bt_l); 86 bt7.setOnClickListener(bt_l); 87 bt8.setOnClickListener(bt_l); 88 bt9.setOnClickListener(bt_l); 89 bt_dian.setOnClickListener(bt_dian1); 90 bt_jia.setOnClickListener(bt_jia1); 91 bt_jian.setOnClickListener(bt_jian1); 92 bt_chu.setOnClickListener(bt_chu1); 93 bt_cheng.setOnClickListener(bt_cheng1); 94 bt_deng.setOnClickListener(bt_deng1); 95 bt_c.setOnClickListener(bt_c1); 96 bt_tui.setOnClickListener(bt_tui1); 97 } 98 class bt_onClick implements View.OnClickListener 99 { 100 @Override 101 public void onClick(View v) { 102 if (s5 == 1) { 103 tv_1.setText(null); 104 s1 = 0; 105 s2 = 0; 106 s3 = 0; 107 s4 = 0; 108 s5 = 0; 109 } 110 String str = tv_1.getText().toString(); 111 //转成按钮 112 Button bt = (Button)v; 113 //取得按钮上的文字 114 String str2 = bt.getText().toString(); 115 str += str2; 116 tv_1.setText(str); 117 } 118 } 119 class bt_dian_onClick implements View.OnClickListener { 120 public void onClick(View v) { 121 if(s5==1) 122 { 123 tv_1.setText(null); 124 s1=0; 125 s2=0; 126 s3=0; 127 s4=0; 128 s5=0; 129 } 130 131 if(s6==1) 132 { 133 return; 134 } 135 Button bt = (Button) v; 136 String str = tv_1.getText().toString(); 137 138 if(str.length()==0||str.equals("0.")){ 139 tv_1.setText("0."); 140 return; 141 } 142 143 String str2 = bt.getText().toString(); 144 str += str2; 145 tv_1.setText(str); 146 s6=1; 147 } 148 } 149 class bt_jia_onClick implements View.OnClickListener 150 { 151 @Override 152 public void onClick(View v) { 153 s5 = 0; 154 String str=tv_1.getText().toString(); 155 if (str.length()==0) 156 { 157 return; 158 } 159 if (s1 == 0) { 160 s1 = Double.parseDouble(str); 161 162 } else { 163 s3 = Double.parseDouble(str); 164 if (s2 == 0) 165 { 166 s1 = s3; 167 } 168 if (s2 == 1) 169 { 170 s1 += s3; 171 } 172 if (s2 == 2) 173 { 174 s1 -= s3; 175 } 176 if (s2 == 3) 177 { 178 s1 *= s3; 179 } 180 if (s2==4&&s3!=0) 181 { 182 s1 /= s3; 183 } 184 if (s2==4&&s3==0) 185 { 186 s1=0; 187 s2=0; 188 s3=0; 189 s4=0; 190 s5=0; 191 } 192 } 193 tv_1.setText(null); 194 s2 = 1; 195 s6=0; 196 } 197 } 198 class bt_jian_onClick implements View.OnClickListener 199 { 200 @Override 201 public void onClick(View v) { 202 s5 = 0; 203 String str=tv_1.getText().toString(); 204 if (str.length()==0) 205 { 206 return; 207 } 208 if (s1 == 0) { 209 s1 = Double.parseDouble(str); 210 211 } else { 212 s3 = Double.parseDouble(str); 213 if (s2 == 0) 214 { 215 s1 = s3; 216 } 217 if (s2 == 1) 218 { 219 s1 += s3; 220 } 221 if (s2 == 2) 222 { 223 s1 -= s3; 224 } 225 if (s2 == 3) 226 { 227 s1 *= s3; 228 } 229 if (s2==4&&s3!=0) 230 { 231 s1 /= s3; 232 } 233 if (s2==4&&s3==0) 234 { 235 s1=0; 236 s2=0; 237 s3=0; 238 s4=0; 239 s5=0; 240 } 241 } 242 tv_1.setText(null); 243 s2 = 2; 244 s6=0; 245 } 246 } 247 class bt_cheng_onClick implements View.OnClickListener 248 { 249 @Override 250 public void onClick(View v) { 251 s5 = 0; 252 String str=tv_1.getText().toString(); 253 if (str.length()==0) 254 { 255 return; 256 } 257 if (s1 == 0) { 258 s1 = Double.parseDouble(str); 259 260 } else { 261 s3 = Double.parseDouble(str); 262 if (s2 == 0) 263 { 264 s1 = s3; 265 } 266 if (s2 == 1) 267 { 268 s1 += s3; 269 } 270 if (s2 == 2) 271 { 272 s1 -= s3; 273 } 274 if (s2 == 3) 275 { 276 s1 *= s3; 277 } 278 if (s2==4&&s3!=0) 279 { 280 s1 /= s3; 281 } 282 if (s2==4&&s3==0) 283 { 284 s1=0; 285 s2=0; 286 s3=0; 287 s4=0; 288 s5=0; 289 } 290 } 291 tv_1.setText(null); 292 s2 = 3; 293 s6=0; 294 } 295 } 296 class bt_chu_onClick implements View.OnClickListener 297 { 298 @Override 299 public void onClick(View v) { 300 s5 = 0; 301 String str=tv_1.getText().toString(); 302 if (str.length()==0) 303 { 304 return; 305 } 306 if (s1 == 0) { 307 s1 = Double.parseDouble(str); 308 309 } else { 310 s3 = Double.parseDouble(str); 311 if (s2 == 0) 312 { 313 s1 = s3; 314 } 315 if (s2 == 1) 316 { 317 s1 += s3; 318 } 319 if (s2 == 2) 320 { 321 s1 -= s3; 322 } 323 if (s2 == 3) 324 { 325 s1 *= s3; 326 } 327 if (s2==4&&s3!=0) 328 { 329 s1 /= s3; 330 } 331 if (s2==4&&s3==0) 332 { 333 s1=0; 334 s2=0; 335 s3=0; 336 s4=0; 337 s5=0; 338 } 339 } 340 tv_1.setText(null); 341 s2 = 4; 342 s6=0; 343 } 344 } 345 class bt_deng_onClick implements View.OnClickListener { 346 public void onClick(View v) { 347 String str = tv_1.getText().toString(); 348 if(str.length()==0) 349 { 350 return; 351 } 352 s3 = Double.parseDouble(str); 353 if(s3==0&&s2==4) 354 { 355 return; 356 } 357 if(s2==0) 358 { 359 s4=s3; 360 } 361 if(s2==1) 362 { 363 s4=s1+s3; 364 } 365 if(s2==2) 366 { 367 s4=s1-s3; 368 } 369 if(s2==3) 370 { 371 s4=s1*s3; 372 } 373 if(s2==4&&s3!=0) 374 { 375 s4=s1/s3; 376 } 377 if (s2==4&&s3==0) 378 { 379 s1=0; 380 s2=0; 381 s3=0; 382 s4=0; 383 s5=0; 384 } 385 String str2=s4+""; 386 tv_1.setText(str2); 387 s5=1; 388 s2=0; 389 s6=0; 390 } 391 } 392 class bt_c_onClick implements View.OnClickListener { 393 public void onClick(View v) { 394 s1=0; 395 s2=0; 396 s3=0; 397 s5=0; 398 s4=0; 399 s6=0; 400 tv_1.setText(null); 401 } 402 } 403 class bt_tui_onClick implements View.OnClickListener { 404 public void onClick(View v) { 405 406 String str = tv_1.getText().toString(); 407 if (str.length() == 0) { 408 return; 409 } 410 String str2 = str.substring(0, str.length() - 1); 411 tv_1.setText(str2); 412 if (s5 == 1) { 413 s5 = 0; 414 } 415 } 416 } 417 }
可实现计算器的基本运算功能
感谢@storm47给予我的帮助