Android - EditText文本观察器如果文本以@或#等特殊字符开头,则会显示不正确的值

我有一篇社论。我已经添加了一个文本观察器来编辑文本。我听到了文本的变化。?如果用户输入以@开头,我会显示用户建议(比如当你输入@时,推特会显示建议)

如果文本以普通字母开头,则一切正常。

例如:

hello @random_user how are you
 @this also works because there is an empty space before '@'

这个例子是可行的。

但是,如果文本以特殊字符开头,text Watcher会显示不正确的值,例如:

@hello_user

#someHashtag

文本监视器返回false值。我正在使用onTextChanged方法来跟踪文本

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    //Text in edittext is '@user' but I get:
    //start = 0, before = 0 and count = 1;
    //edittext.getSelectionStart() also returns 1 but cursor is at the end of the line.
    //edittext.getText().toString().length() also returns 1 but @user is 5 length.
}

我该如何解决这个问题呢?

编辑:edittext.postdesc.getText().toString()只返回第一个字符。例如,如果我的文本是“@user”,则getText方法只返回@

转载请注明出处:http://www.intsu.net/article/20230507/2020282.html