MultiClickTextView.java
2.6 KB
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
package com.sw.laryngoscope.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
/**
* @Description 多次点击
* @Author
* @Time 2020/06/09
*/
@SuppressLint("AppCompatCustomView")
public class MultiClickTextView extends TextView {
/** 长度决定点击的次数*/
private long[] mHints;
/** 多少秒内点完触发*/
private long limiit = 5000;
/** 连续点击次数*/
private int mCount = 10;
private OnMultiClickListener multiClickListener;
public MultiClickTextView(Context context) {
super(context);
init();
}
public MultiClickTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MultiClickTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mCount = 10;
limiit = 5 * 1000L;
init();
}
private void init(){
mHints = new long[mCount];
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.arraycopy(mHints, 1, mHints, 0, mHints.length - 1);
mHints[mHints.length - 1] = SystemClock.uptimeMillis();
if (mHints[0] >= (SystemClock.uptimeMillis() - limiit)) {
if (multiClickListener != null) {
mHints = new long[mCount];
multiClickListener.onMultiClick();
}
}
}
});
}
/**
* 设置点击次数
* @param count
*/
public void setClickCount(int count){
if(count < 1){
throw new RuntimeException("多次点击请大于1次好吧!!!");
}
mCount = count;
mHints = new long[mCount];
}
/**
* 设置总时间
* @param time 秒
*/
public void setClickLimit(long time){
if(time < 1){
throw new RuntimeException("你设置那么小你能点得了???");
}
limiit = time * 1000L;
}
public void reset(){
mHints = new long[mCount];
}
public void setOnMultiClickListener(OnMultiClickListener listener){
multiClickListener = listener;
}
public interface OnMultiClickListener{
void onMultiClick();
}
}