标签:
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 package com.example.administrator.testapp; 2 3 import android.os.Message; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.os.Handler; 8 import android.widget.Button; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 import java.util.Random; 12 import java.util.logging.LogRecord; 13 14 public class test_activity6 extends AppCompatActivity { 15 16 TextView tv_7; 17 18 Button bt_3,bt_4,bt_5; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test_activity6); 24 25 tv_7 = (TextView) findViewById(R.id.tv_7); 26 bt_3 = (Button) findViewById(R.id.bt_3); 27 bt_4 = (Button) findViewById(R.id.bt_4); 28 bt_5 = (Button) findViewById(R.id.bt_5); 29 } 30 31 public void bt3_onclick(View v) 32 { 33 switch (v.getId()) 34 { 35 case R.id.bt_3: 36 //发送增加消息 37 hl.sendEmptyMessage(1); 38 break; 39 case R.id.bt_4: 40 //发送减少消息 41 hl.sendEmptyMessage(2); 42 break; 43 case R.id.bt_5: 44 //发送暂停消息 45 hl.sendEmptyMessage(3); 46 break; 47 } 48 int i = 10; 49 Handler hl = new Handler() 50 { 51 @Override 52 public void handleMessage(Message msg) { 53 super.handleMessage(msg); 54 switch (msg.what) 55 { 56 //增加 57 case 1: 58 if (i==20) 59 { 60 Toast.makeText(test_activity6.this, "无法增加!", Toast.LENGTH_SHORT).show(); 61 return; 62 } 63 i++; 64 tv_7.setText(i + ""); 65 //发送 66 hl.sendEmptyMessageDelayed(1, 1000); 67 hl.removeMessages(2); 68 if (i>10) 69 { 70 bt_3.setClickable(false); 71 } 72 break; 73 //减少 74 case 2: 75 if (i==1) 76 { 77 return; 78 } 79 i--; 80 tv_7.setText(i+""); 81 hl.sendEmptyMessageDelayed(2,1000); 82 hl.removeMessages(1); 83 if (i<10) 84 { 85 bt_4.setClickable(false); 86 } 87 else 88 break; 89 //暂停 90 case 3: 91 hl.removeMessages(1); 92 hl.removeMessages(2); 93 Toast.makeText(test_activity6.this, "完成暂停!", Toast.LENGTH_SHORT).show(); 94 break; 95 } 96 } 97 };
标签:
原文地址:http://www.cnblogs.com/TENOKAWA/p/5499511.html