Skip to content

Commit a65d29a

Browse files
committed
Fixed backspace on Google Keyboard.
1 parent 2d8813f commit a65d29a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

library/src/main/java/me/gujun/android/taggroup/TagGroup.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import android.view.View;
2525
import android.view.ViewGroup;
2626
import android.view.inputmethod.EditorInfo;
27+
import android.view.inputmethod.InputConnection;
28+
import android.view.inputmethod.InputConnectionWrapper;
2729
import android.widget.TextView;
2830

2931
import java.util.ArrayList;
@@ -1094,5 +1096,31 @@ public boolean onTouchEvent(MotionEvent event) {
10941096
}
10951097
return true;
10961098
}
1099+
1100+
@Override
1101+
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
1102+
return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), true);
1103+
}
1104+
1105+
/**
1106+
* Solve edit text delete(backspace) key detect, see<a href="http://stackoverflow.com/a/14561345/3790554">
1107+
* Android: Backspace in WebView/BaseInputConnection</a>
1108+
*/
1109+
private class ZanyInputConnection extends InputConnectionWrapper {
1110+
public ZanyInputConnection(android.view.inputmethod.InputConnection target, boolean mutable) {
1111+
super(target, mutable);
1112+
}
1113+
1114+
@Override
1115+
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
1116+
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
1117+
if (beforeLength == 1 && afterLength == 0) {
1118+
// backspace
1119+
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
1120+
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
1121+
}
1122+
return super.deleteSurroundingText(beforeLength, afterLength);
1123+
}
1124+
}
10971125
}
10981126
}

0 commit comments

Comments
 (0)