import android.text.TextUtils; //导入方法依赖的package包/类
/**
* Autocompletes the text on the url bar and selects the text that was not entered by the
* user. Using append() instead of setText() to preserve the soft-keyboard layout.
* @param userText user The text entered by the user.
* @param inlineAutocompleteText The suggested autocompletion for the user's text.
*/
public void setAutocompleteText(CharSequence userText, CharSequence inlineAutocompleteText) {
boolean emptyAutocomplete = TextUtils.isEmpty(inlineAutocompleteText);
if (!emptyAutocomplete) mDisableTextScrollingFromAutocomplete = true;
int autocompleteIndex = userText.length();
String previousText = getQueryText();
CharSequence newText = TextUtils.concat(userText, inlineAutocompleteText);
setIgnoreTextChangesForAutocomplete(true);
mDisableTextAccessibilityEvents = true;
if (!TextUtils.equals(previousText, newText)) {
// The previous text may also have included autocomplete text, so we only
// append the new autocomplete text that has changed.
if (TextUtils.indexOf(newText, previousText) == 0) {
append(newText.subSequence(previousText.length(), newText.length()));
} else {
setUrl(newText.toString(), null);
}
}
if (getSelectionStart() != autocompleteIndex
|| getSelectionEnd() != getText().length()) {
setSelection(autocompleteIndex, getText().length());
if (inlineAutocompleteText.length() != 0) {
// Sending a TYPE_VIEW_TEXT_SELECTION_CHANGED accessibility event causes the
// previous TYPE_VIEW_TEXT_CHANGED event to be swallowed. As a result the user
// hears the autocomplete text but *not* the text they typed. Instead we send a
// TYPE_ANNOUNCEMENT event, which doesn't swallow the text-changed event.
announceForAccessibility(inlineAutocompleteText);
}
}
if (emptyAutocomplete) {
mAutocompleteSpan.clearSpan();
} else {
mAutocompleteSpan.setSpan(userText, inlineAutocompleteText);
}
setIgnoreTextChangesForAutocomplete(false);
mDisableTextAccessibilityEvents = false;
}