-
Notifications
You must be signed in to change notification settings - Fork 155
/
BetterSpinner.java
69 lines (56 loc) · 1.96 KB
/
BetterSpinner.java
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
package com.weiwangcn.betterspinner.library;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import java.util.Calendar;
/**
* Created by Wei on 2015/3/23.
*/
public class BetterSpinner extends AutoCompleteTextView {
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
public BetterSpinner(Context context) {
super(context);
}
public BetterSpinner(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public BetterSpinner(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering("", 0); //show everything in the list
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
setKeyListener(null);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration < MAX_CLICK_DURATION) {
requestFocus();
showDropDown();
}
}
}
return super.onTouchEvent(event);
}
}