我有 EditText,我可以在其中写词、主题标签和标签。我用 Linkify 识别所有标签或主题标签,当我用这种方法按下按钮时,我可以得到所有它们:
String text = editText.getText().toString();
String[] words = text.split(" ");
List<String> hashtags = new ArrayList<String>();
List<String> tags = new ArrayList<String>();
for ( String word : words) {
if (word.substring(0, 1).equals("#")) {
Log.i("HASHTAG", word);
hashtags.add(word);
}
if (word.substring(0, 1).equals("@")) {
Log.i("TAG", word);
tags.add(word);
}
}
现在我想在写作时获取标签,并向我的数据库发出请求,以便向用户建议一些词。我如何获取我插入的主题标签的一部分然后进行搜索?
例如,如果我写:“我认为@ta” 我想使用“ta”并在我的数据库中搜索它,然后我将使用以下内容填充我的 ListView :“tank”“take”“taste”...
最佳答案
您可以添加 TextChangedListener像这样:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
String text = s.toString()
if (text.lastIndexOf("@") > text.lastIndexOf(" ")){
String tag = text.substring(text.lastIndexOf("@"), text.length());
// search for tag...
}
}
});
关于android - 识别编辑文本中的当前主题标签或标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31725289/