【问题标题】:Recognize current hashtag or tag in edittext在edittext中识别当前的主题标签或标签
【发布时间】:2015-10-21 21:36:22
【问题描述】:

我有和 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”...

【问题讨论】:

    标签: android mysql tags android-edittext hashtag


    【解决方案1】:

    您可以像这样添加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...
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多