java concat代码_Java TextUtils.concat方法代码示例

该段代码展示了在Android中如何使用TextUtils.concat方法完成自动补全文本的功能。当用户输入文本时,concat方法用于连接用户文本和建议的自动补全文本,同时处理了软键盘布局、文本选择、无障碍事件等细节。
摘要由CSDN通过智能技术生成

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值