增加、减少、暂停

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp2.TestActivity6"
11     android:orientation="vertical">
12  <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="10"
16         android:id="@+id/tv_6"
17         android:layout_gravity="center"/>
18 
19     <Button
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="增加"
23         android:onClick="bt3_OnClick"
24         android:id="@+id/bt_3"/>
25 
26     <Button
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:text="减少"
30         android:onClick="bt3_OnClick"
31         android:id="@+id/bt_4"/>
32 
33     <Button
34         android:layout_width="match_parent"
35         android:layout_height="wrap_content"
36         android:text="暂停"
37         android:onClick="bt3_OnClick"
38         android:id="@+id/bt_5"
39         android:enabled="false"/>
40 
41 </LinearLayout>
  1 package com.hanqi.testapp2;
  2 
  3 import android.os.Bundle;
  4 import android.os.Handler;
  5 import android.os.Message;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.widget.Button;
  9 import android.widget.TextView;
 10 import android.widget.Toast;
 11 
 12 import java.util.Random;
 13 
 14 
 15 public class TestActivity6 extends AppCompatActivity {
 16 
 17     TextView tv_3;
 18     TextView tv_4;
 19     TextView tv_5,tv_6;
 20     Button bt_3,bt_4,bt_5;
 21     //定义Handler
 22     Handler h=new Handler(){
 23         @Override
 24         public void handleMessage(Message msg) {
 25             super.handleMessage(msg);
 26 
 27             //处理消息
 28             if (msg.what==1)
 29             {
 30                 String m=msg.obj.toString();
 31 
 32                 tv_5.setText(tv_5.getText()+" "+m);
 33             }
 34             else if (msg.what==2)
 35             {
 36                 tv_5.setText(tv_5.getText()+"空消息");
 37             }
 38 
 39         }
 40     };
 41 
 42     int i=10;
 43 
 44     Handler hl=new Handler()
 45     {
 46         @Override
 47         public void handleMessage(Message msg) {
 48             super.handleMessage(msg);
 49 
 50             switch (msg.what)
 51             {
 52                 case 1:
 53                     bt_3.setEnabled(false);
 54                     bt_4.setEnabled(true);
 55                     bt_5.setEnabled(true);
 56                     if (i==20)
 57                     {
 58                        bt_5.setEnabled(false);
 59                         return;
 60                     }
 61 
 62                     i++;
 63 
 64 
 65                     tv_6.setText(i + "");
 66 
 67 
 68 
 69                     //发送
 70                     hl.sendEmptyMessageDelayed(1,1000);
 71 
 72                     hl.removeMessages(2);
 73 
 74                     break;
 75 
 76                 case 2:
 77                     bt_3.setEnabled(true);
 78                     bt_4.setEnabled(false);
 79                     bt_5.setEnabled(true);
 80                     if (i==1)
 81                     {
 82                         bt_5.setEnabled(false);
 83                         return;
 84                     }
 85 
 86                     i--;
 87 
 88 
 89                     tv_6.setText(i+"");
 90 
 91                     //发送
 92                     hl.sendEmptyMessageDelayed(2,1000);
 93 
 94                     hl.removeMessages(1);
 95 
 96                     break;
 97 
 98                 case 3:
 99                     bt_3.setEnabled(true);
100                     bt_4.setEnabled(true);
101                     bt_5.setEnabled(false);
102 
103                     hl.removeMessages(1);
104                     hl.removeMessages(2);
105 
106                     break;
107             }
108         }
109     };
110 
111     @Override
112     protected void onCreate(Bundle savedInstanceState) {
113         super.onCreate(savedInstanceState);
114         setContentView(R.layout.activity_test6);
115 
116         tv_3=(TextView)findViewById(R.id.tv_3);
117         tv_4=(TextView)findViewById(R.id.tv_4);
118         tv_5=(TextView)findViewById(R.id.tv_5);
119         tv_6=(TextView)findViewById(R.id.tv_6);
120         bt_3=(Button)findViewById(R.id.bt_3);
121         bt_4=(Button)findViewById(R.id.bt_4);
122         bt_5=(Button)findViewById(R.id.bt_5);
123 
124 
125     }
126 
127     //3个按钮
128     public void bt3_OnClick(View v)
129     {
130         switch (v.getId())
131         {
132             case R.id.bt_3:
133 
134                 //发送增加消息
135                 hl.sendEmptyMessage(1);
136 
137                 break;
138             case R.id.bt_4:
139 
140                 //发送减少消息
141                 hl.sendEmptyMessage(2);
142 
143                 break;
144             case R.id.bt_5:
145 
146                 //发送暂停消息
147                 hl.sendEmptyMessage(3);
148 
149                 break;
150 
151         }
152     }
153 }

 

  

  

原文地址:https://www.cnblogs.com/cycanfly/p/5501185.html