diff --git a/README.md b/README.md index b86d8bdc..b9aff7e9 100644 --- a/README.md +++ b/README.md @@ -380,6 +380,10 @@ All tokens will turn into the partial completion text they replaced Tokens will be replaced with the toString value of the objects they represent when they are deleted +#### TokenCompleteTextView.TokenDeleteStyle.SelectThenDelete + +This mode will first select the token and when pressing the 'delete' key again it'll delete the selected token + Restoring the view state ======================== diff --git a/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java b/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java index 63ab9821..d7f97933 100644 --- a/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java +++ b/library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java @@ -61,7 +61,8 @@ public enum TokenDeleteStyle { _Parent, //...do the parent behavior, not recommended Clear, //...clear the underlying text PartialCompletion, //...return the original text used for completion - ToString //...replace the token with toString of the token object + ToString, //...replace the token with toString of the token object + SelectThenDelete //...first backspace selects the token and another backspace deletes it } //When the user clicks on a token... @@ -835,6 +836,7 @@ protected CharSequence convertSelectionToString(Object object) { //if the token gets deleted, this text will get put in the field instead switch (deletionStyle) { case Clear: + case SelectThenDelete: return ""; case PartialCompletion: return currentCompletionText(); @@ -1517,7 +1519,23 @@ public boolean canDeleteSelection(int beforeLength) { int endTokenSelection = text.getSpanEnd(span); // moving on, no need to check this token - if (isTokenRemovable(span.token)) continue; + if (isTokenRemovable(span.token)) { + if (deletionStyle == TokenDeleteStyle.SelectThenDelete) { + if (span.view.isSelected()) { + // If a token is already selected then we can just delete it + return true; + } + // If we're not selecting multiple characters and we want to remove the current token + if (startSelection == endSelection && endTokenSelection + 1 == endSelection && !span.view.isSelected()) { + // Just select it and don't delete it, the next backspace will delete it + clearSelections(); + span.view.setSelected(true); + invalidate(); + return false; + } + } + continue; + } if (startSelection == endSelection) { // Delete single @@ -1552,10 +1570,8 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) { //Shouldn't be able to delete prefix, so don't do anything if (getSelectionStart() <= prefix.length()) { beforeLength = 0; - return deleteSelectedObject(false) || super.deleteSurroundingText(beforeLength, afterLength); } - - return super.deleteSurroundingText(beforeLength, afterLength); + return deleteSelectedObject(false) || super.deleteSurroundingText(beforeLength, afterLength); } } }