Skip to content

Instantly share code, notes, and snippets.

@damonliuchn
Created September 23, 2015 08:25
Show Gist options
  • Save damonliuchn/f4e0fc7b07ff6f5e0e5f to your computer and use it in GitHub Desktop.
Save damonliuchn/f4e0fc7b07ff6f5e0e5f to your computer and use it in GitHub Desktop.
public class InputInterceptEditText extends EditText {
private InputInterceptListener inputInterceptListener;
public InputInterceptEditText(Context context) {
super(context);
}
public InputInterceptEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InputInterceptEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public InputInterceptEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection con = super.onCreateInputConnection(outAttrs);
return new EditTextInputConnection(con, true, inputInterceptListener);
}
private class EditTextInputConnection extends InputConnectionWrapper {
private InputInterceptListener inputInterceptListener;
public EditTextInputConnection(InputConnection target, boolean mutable, InputInterceptListener listener) {
super(target, mutable);
this.inputInterceptListener = listener;
}
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
android.util.Log.i("LICW", String.format("commitText(%d, %d)", YcEditText.this.getSelectionStart(), newCursorPosition));
boolean isIntercept = false;
if (inputInterceptListener != null) {
isIntercept = inputInterceptListener.interceptInput(getText().toString() + text, text);
}
return isIntercept ? false : super.commitText(text, newCursorPosition);
}
@Override
public int getCursorCapsMode(int reqModes) {
android.util.Log.i("LICW", String.format("getCursorCapsMode(%d)", reqModes));
if (reqModes == TextUtils.CAP_MODE_SENTENCES && inputInterceptListener != null) {
inputInterceptListener.inputComplete();
}
return super.getCursorCapsMode(reqModes);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
android.util.Log.i("LICW", String.format("sendKeyEvent(%s)", event));
boolean isIntercept = false;
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
if (inputInterceptListener != null) {
String newStr = YcEditText.this.getText().toString();
if (newStr.length() > 0) {
isIntercept = inputInterceptListener.interceptDelete(newStr.substring(0, newStr.length() - 1));
}
}
}
return isIntercept ? false : super.sendKeyEvent(event);
}
}
public void setAcceptedChars(final String chars) {
setKeyListener(new NumberKeyListener() {
@Override
protected char[] getAcceptedChars() {
return chars.toCharArray();
}
@Override
public int getInputType() {
return EditorInfo.TYPE_CLASS_NUMBER;
}
});
}
public InputInterceptListener getInputInterceptListener() {
return inputInterceptListener;
}
public void setInputInterceptListener(InputInterceptListener inputInterceptListener) {
this.inputInterceptListener = inputInterceptListener;
}
public interface InputInterceptListener {
boolean interceptInput(String newStr, CharSequence text);
boolean interceptDelete(String newStr);
void inputComplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment