识别当前的hashtag或edittext中的标签

diegocom

我有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”并在数据库中搜索它,然后在列表视图中填充:“ 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...
            }
        }
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章