From 97ff5fdaf33ed20277ab7a2e7ad756ec7083e284 Mon Sep 17 00:00:00 2001 From: Matt Chowning Date: Tue, 13 May 2014 14:41:37 -0400 Subject: [PATCH] Added InputConnectionWrapper to enable soft keyboard delete of selected object --- .../TokenCompleteTextView.java | 53 ++++++++++++++----- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java b/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java index 8645e169..dcf62120 100644 --- a/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java +++ b/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java @@ -31,6 +31,7 @@ import android.view.ViewGroup.LayoutParams; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; +import android.view.inputmethod.InputConnectionWrapper; import android.widget.Filter; import android.widget.ListView; import android.widget.MultiAutoCompleteTextView; @@ -306,7 +307,7 @@ public void performCompletion() { @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { //Override normal multiline text handling of enter/done and force a done button - InputConnection connection = super.onCreateInputConnection(outAttrs); + TokenInputConnection connection = new TokenInputConnection(super.onCreateInputConnection(outAttrs), true); int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION; if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) { // clear the existing action @@ -363,24 +364,30 @@ public boolean onKeyDown(int keyCode, KeyEvent event) { } break; case KeyEvent.KEYCODE_DEL: - if (tokenClickStyle != null && tokenClickStyle.isSelectable()) { - Editable text = getText(); - if (text == null) break; - - TokenImageSpan[] spans = text.getSpans(0, text.length(), TokenImageSpan.class); - for (TokenImageSpan span: spans) { - if (span.view.isSelected()) { - removeSpan(span); - handled = true; - break; - } - } - } + handled = deleteSelectedObject(handled); + break; } return handled || super.onKeyDown(keyCode, event); } + private boolean deleteSelectedObject(boolean handled) { + if (tokenClickStyle != null && tokenClickStyle.isSelectable()) { + Editable text = getText(); + if (text == null) return handled; + + TokenImageSpan[] spans = text.getSpans(0, text.length(), TokenImageSpan.class); + for (TokenImageSpan span: spans) { + if (span.view.isSelected()) { + removeSpan(span); + handled = true; + break; + } + } + } + return handled; + } + @Override public boolean onEditorAction(TextView view, int action, KeyEvent keyEvent) { if (action == EditorInfo.IME_ACTION_DONE) { @@ -1224,4 +1231,22 @@ public SavedState[] newArray(int size) { } }; } + + private class TokenInputConnection extends InputConnectionWrapper { + + public TokenInputConnection(InputConnection target, boolean mutable) { + super(target, mutable); + } + + // This will fire if the soft keyboard delete key is pressed. + // The onKeyPressed method does not always do this. + @Override + public boolean deleteSurroundingText(int beforeLength, int afterLength) { + if (deleteSelectedObject(false)) { + return true; + } else { + return super.deleteSurroundingText(beforeLength, afterLength); + } + } + } }