Blame view

src/com/ectrip/trips/view/LongClickButton.java 3.19 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  package com.ectrip.trips.view;
  
  import java.lang.ref.WeakReference;
  
  import android.content.Context;
  import android.os.Handler;
  import android.os.Message;
  import android.os.SystemClock;
  import android.util.AttributeSet;
  import android.view.View;
  import android.widget.Button;
  
  /**
   * 长按连续响应的Button
   * @author jigo
   */
  public class LongClickButton extends Button {
      /**
       * 长按连续响应的监听,长按时将会多次调用该接口中的方法直到长按结束
       */
      private LongClickRepeatListener repeatListener;
      /**
       * 间隔时间(ms
       */
      private long intervalTime;
  
      private MyHandler handler;
  
      public LongClickButton(Context context) {
          super(context);
  
          init();
      }
  
      public LongClickButton(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          init();
      }
  
      public LongClickButton(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
  
          init();
      }
  
      /**
       * 初始化监听
       */
      private void init() {
          handler = new MyHandler(this);
          setOnLongClickListener(new OnLongClickListener() {
              @Override
              public boolean onLongClick(View v) {
                  new Thread(new LongClickThread()).start();
                  return true;
              }
          });
      }
  
      /**
       * 长按时,该线程将会启动
       */
      private class LongClickThread implements Runnable {
          private int num;
  
          @Override
          public void run() {
              while (LongClickButton.this.isPressed()) {
                  num++;
                  if (num % 5 == 0) {
                      handler.sendEmptyMessage(1);
                  }
  
                  SystemClock.sleep(intervalTime / 5);
              }
          }
      }
  
      /**
       * 通过handler,使监听的事件响应在主线程中进行
       */
      private static class MyHandler extends Handler {
          private WeakReference ref;
  
          MyHandler(LongClickButton button) {
              ref = new WeakReference(button);
          }
  
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
  
              LongClickButton button = (LongClickButton) ref.get();
              if (button != null && button.repeatListener != null) {
                  button.repeatListener.repeatAction();
              }
          }
      }
  
      /**
       * 设置长按连续响应的监听和间隔时间,长按时将会多次调用该接口中的方法直到长按结束
       *
       * @param listener     监听
       * @param intervalTime 间隔时间(ms
       */
      public void setLongClickRepeatListener(LongClickRepeatListener listener, long intervalTime) {
          this.repeatListener = listener;
          this.intervalTime = intervalTime;
      }
  
      /**
       * 设置长按连续响应的监听(使用默认间隔时间100ms),长按时将会多次调用该接口中的方法直到长按结束
       *
       * @param listener 监听
       */
      public void setLongClickRepeatListener(LongClickRepeatListener listener) {
          setLongClickRepeatListener(listener, 100);
      }
  
      public interface LongClickRepeatListener {
          void repeatAction();
      }
  }