https://gitee.com/Pino_W/ait.git
记录一下
mBinding.atEdittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// 从start开始的count个字符将会被一个新的长度为after的文本替换,注意这里是将被替换,还没有被替换
Editable editable = mBinding.atEdittext.getText();
// 光标起始为止如果不是在最后,那对于在光标后面的文字块都要做偏移
if (start < editable.length()) {
int end = start + count;
int offset = after - count;
whenDelText(start, end, offset);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 1 && !TextUtils.isEmpty(s)) {
char mentionChar = s.toString().charAt(start);
int selectionStart = mBinding.atEdittext.getSelectionStart();
if (mentionChar == '@') {
mPresenter.getData(page, pageNum);
// 这里是把刚刚输入的@删除掉
mBinding.atEdittext.getText().delete(selectionStart - 1, selectionStart);
}
// else if (mentionChar == ‘#’) {
// startActivityForResult(TopicListActivity.getTopicListIntent(MainActivity.this), REQUEST_TAG_APPEND);
// et_view.getText().delete(selectionStart - 1, selectionStart);
// }
}
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
setBtnBg();
} else {
if (videoList.size() <= 0 && photoAdapter.getmList().size() <= 0 && mVicePath == null) {
setBtnBgNormal();
}
}
}
});
这篇博客介绍了如何在Android中自定义一个@输入框,通过TextWatcher监听文本变化,实现光标位置的动态调整。内容包括在文本改变时处理光标偏移的逻辑,并提到了对#话题的处理方式。

660

被折叠的 条评论
为什么被折叠?



