public class CharPolicy extends java.lang.Object implements AutocompletePolicy
AutocompletePolicy for cases when you want to trigger the popup when a
certain character is shown.
For instance, this might be the case for hashtags ('#') or usernames ('@') or whatever you wish.
Passing this to Autocomplete.Builder ensures the following behavior (assuming '@'):
- text "@john" : presenter will be passed the query "john"
- text "You should see this @j" : presenter will be passed the query "j"
- text "You should see this @john @m" : presenter will be passed the query "m"| Constructor and Description |
|---|
CharPolicy(char trigger)
Constructs a char policy for the given character.
|
CharPolicy(char trigger,
boolean needSpaceBefore)
Constructs a char policy for the given character.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.CharSequence |
getQuery(android.text.Spannable text)
Called to understand which query should be passed to
AutocompletePresenter
for a showing popup. |
static int[] |
getQueryRange(android.text.Spannable text)
Returns the current query out of the given Spannable.
|
protected boolean |
isValidChar(char ch)
Can be overriden to understand which characters are valid.
|
void |
onDismiss(android.text.Spannable text)
Called when popup is dismissed.
|
boolean |
shouldDismissPopup(android.text.Spannable text,
int cursorPos)
Called to understand whether a currently shown popup should be closed, maybe
because text is invalid.
|
boolean |
shouldShowPopup(android.text.Spannable text,
int cursorPos)
Called to understand whether the popup should be shown.
|
public CharPolicy(char trigger)
trigger - the triggering character.public CharPolicy(char trigger,
boolean needSpaceBefore)
trigger - the triggering character.needSpaceBefore - whether we need a space before triggerprotected boolean isValidChar(char ch)
ch - the characterpublic boolean shouldShowPopup(android.text.Spannable text,
int cursorPos)
AutocompletePolicyreturn text.length() > 0
- Show when last char is @: return text.getCharAt(text.length()-1) == '@'shouldShowPopup in interface AutocompletePolicytext - current text, along with its SpanscursorPos - the position of the cursorpublic boolean shouldDismissPopup(android.text.Spannable text,
int cursorPos)
AutocompletePolicyreturn !shouldShowPopup(text, cursorPos).
However this is defined so you can add or clear spans.shouldDismissPopup in interface AutocompletePolicytext - current text, along with its SpanscursorPos - the position of the cursorpublic java.lang.CharSequence getQuery(android.text.Spannable text)
AutocompletePolicyAutocompletePresenter
for a showing popup. If this is called, AutocompletePolicy.shouldShowPopup(Spannable, int) just returned
true, or AutocompletePolicy.shouldDismissPopup(Spannable, int) just returned false.
This is useful to understand which part of the text should be passed to presenters.
For example, user might have typed '@john' to select a username, but you just want to
search for 'john'.
For more complex cases, you can add inclusive Spans in AutocompletePolicy.shouldShowPopup(Spannable, int),
and get the span position here.getQuery in interface AutocompletePolicytext - current text, along with its Spanspublic void onDismiss(android.text.Spannable text)
AutocompletePolicyonDismiss in interface AutocompletePolicytext - text at the moment of dismissing@Nullable public static int[] getQueryRange(android.text.Spannable text)
text - the anchor text