-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditTextPopUpFixed
90 lines (75 loc) · 2.74 KB
/
EditTextPopUpFixed
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
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
/**
* Created by Jaume Colom Ferrer on 14/12/2015
*/
public class EditTextPopUpFixed extends AppCompatEditText {
private static final int TIME_SLEEP = 500;
private Context ctx;
private EditTextListener mListener;
private int backTimes = 0;
public EditTextPopUpFixed(Context context) {
super(context);
ctx = context;
}
public EditTextPopUpFixed(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
}
public EditTextPopUpFixed(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ctx = context;
}
public void setEditTextListener(EditTextListener lis) {
mListener = lis;
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
backTimes = 0;
return false;
}
});
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
backTimes++;
if (backTimes == 3) {
backTimes = 0;
mListener.close();
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ENTER) {
InputMethodManager mgr = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
if (mgr.isActive()) {
mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
boolean mustRevalidate = getError() != null;
setError(null);
if (mListener != null && mustRevalidate) {
Handler han = new Handler();
han.postDelayed(new Runnable() {
@Override
public void run() {
mListener.revalidateEditText();
}
}, TIME_SLEEP);
}
} else {
backTimes = 0;
super.onKeyPreIme(keyCode, event);
}
return true;
}
return false;
}
public interface EditTextListener {
void revalidateEditText(); //Revalidates the EditText to refresh the Popup (Must revalidate the EditText in its implementation)
void close(); // Method to close the activity or fragment (Must finish activity or go back in its implementation)
}
}