1.AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.testapp"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity"></activity> 12 <activity android:name=".test_activity6"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 </application> 19 20 </manifest>
2.界面布局代码
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 tools:context="com.example.administrator.testapp.test_activity6" 8 android:orientation="vertical"> 9 10 <TextView 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:text="10" 14 android:textSize="30dp" 15 android:id="@+id/tv_7" 16 android:layout_gravity="center"/> 17 <Button 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="增加" 21 android:textSize="30dp" 22 android:onClick="bt3_onclick" 23 android:id="@+id/bt_3"/> 24 <Button 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:text="减少" 28 android:textSize="30dp" 29 android:onClick="bt3_onclick" 30 android:id="@+id/bt_4"/> 31 <Button 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:text="暂停" 35 android:textSize="30dp" 36 android:onClick="bt3_onclick" 37 android:id="@+id/bt_5"/> 38 </LinearLayout>
3.代码运行程序
1 int i = 10; 2 Handler hl = new Handler() 3 { 4 @Override 5 public void handleMessage(Message msg) { 6 super.handleMessage(msg); 7 switch (msg.what) 8 { 9 //增加 10 case 1: 11 12 bt_3.setEnabled(false); 13 bt_4.setEnabled(true); 14 bt_5.setEnabled(true); 15 16 if (i==20) 17 { 18 bt_5.setEnabled(false); 19 Toast.makeText(test_activity6.this, "无法增加!", Toast.LENGTH_SHORT).show(); 20 return; 21 } 22 i++; 23 tv_7.setText(i + ""); 24 //发送 25 hl.sendEmptyMessageDelayed(1, 1000); 26 hl.removeMessages(2); 27 if (i>10) 28 { 29 bt_3.setEnabled(false); 30 } 31 break; 32 //减少 33 case 2: 34 bt_3.setEnabled(true); 35 bt_4.setEnabled(false); 36 bt_5.setEnabled(true); 37 if (i==1) 38 { 39 bt_5.setEnabled(false); 40 Toast.makeText(test_activity6.this, "无法减少!", Toast.LENGTH_SHORT).show(); 41 return; 42 } 43 i--; 44 tv_7.setText(i+""); 45 hl.sendEmptyMessageDelayed(2,1000); 46 hl.removeMessages(1); 47 if (i<10) 48 { 49 bt_4.setEnabled(false); 50 } else 51 break; 52 //暂停 53 case 3: 54 bt_3.setEnabled(true); 55 bt_4.setEnabled(true); 56 bt_5.setEnabled(false); 57 hl.removeMessages(1); 58 hl.removeMessages(2); 59 Toast.makeText(test_activity6.this, "完成暂停!", Toast.LENGTH_SHORT).show(); 60 break; 61 } 62 } 63 };