三按钮的控制及实效。

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

posted on 2016-05-17 12:05  安然罒  阅读(131)  评论(0编辑  收藏  举报