如何在editText中检测特定字符的文本?

时间:2018-03-22 06:04:27

标签: android android-edittext

我是android的初学者,所以请原谅我,如果我的问题很愚蠢。基本上我想要检测的是的意思,例如用户正在输入abc #hello,只有#hello才会在文字更改中加入。所以我尝试从github代码中获取引用,并且能够打印所有#tags ,但是如果用户正在键入abc #hello #hi #bye //here current tag is #bye,我想只覆盖当前标记意味着我只想烘烤当前标记到飞行中的空间发生。我想知道如何修改我的代码以获得所需的结果。

代码:

editTxt.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) {
            if(s.length()>0)
            showTags(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


   //Methods
    private void showTags(CharSequence text) {

    int startIndexOfNextHashSign;

    int index = 0;
    while (index < text.length()-  1){
        sign = text.charAt(index);
        int nextNotLetterDigitCharIndex = index + 1; // we assume it is next. if if was not changed by findNextValidHashTagChar then index will be incremented by 1
        if(sign=='#'){
            startIndexOfNextHashSign = index;

            nextNotLetterDigitCharIndex = findNextValidHashTagChar(text, startIndexOfNextHashSign);
            Toast.makeText(this,text.subSequence(startIndexOfNextHashSign,nextNotLetterDigitCharIndex),Toast.LENGTH_LONG).show();
            //setColorForHashTagToTheEnd(startIndexOfNextHashSign, nextNotLetterDigitCharIndex);
        }

        index = nextNotLetterDigitCharIndex;
    }
}


private int findNextValidHashTagChar(CharSequence text, int start) {

    int nonLetterDigitCharIndex = -1; // skip first sign '#"
    for (int index = start + 1; index < text.length(); index++) {

        char sign = text.charAt(index);

        boolean isValidSign = Character.isLetterOrDigit(sign) || mAdditionalHashTagChars.contains(sign);
        if (!isValidSign) {
            nonLetterDigitCharIndex = index;
            break;
        }
    }
    if (nonLetterDigitCharIndex == -1) {
        // we didn't find non-letter. We are at the end of text
        nonLetterDigitCharIndex = text.length();
    }

    return nonLetterDigitCharIndex;
}

Github Project

4 个答案:

答案 0 :(得分:5)

试试这个

String sampleText = "abc #hello #hi #bye";
String[] wordSplit = sampleText.split(" ");

for (int i = wordSplit.length-1; i >= 0; i--){
   if(wordSplit[i].contains("#")){
        Toast.makeText(getContext(), wordSplit[i].substring(wordSplit[i].indexOf("#")), Toast.LENGTH_SHORT).show();
        break;
   }
}

编辑:尝试使用此而不是indexOf

lastIndexOf("#")

答案 1 :(得分:0)

作为对above answer的引用,虽然它是一个很好的答案,但它总是干杯最后一个标签意味着如果输入的文字为abc #hi #hello,我们就举个例子即使我们将光标移动到#hi并将其更改为#bye,它也会覆盖las标记,即#hello所以为了解决这个问题,我们可以使用当前光标位置

所以最终的代码是:

editTxt.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 sampleText = s.toString().substring(0,editTxt.getSelectionStart());
            String[] wordSplit = sampleText.split(" ");
            String sign=null;
            for (int i = wordSplit.length-1; i >= 0; i--){
             if(wordSplit[i].contains("#")){
       Toast.makeText(getApplicationContext(), wordSplit[i].substring(wordSplit[i].lastIndexOf("#")), Toast.LENGTH_SHORT).show();
                     break;
                }
            }

        }
    });

答案 2 :(得分:-1)

因此,您的TextWatcher将如下所示

editTxt.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[] wordSplit = s.toString().split(" ");

            for (int i = wordSplit.length-1; i >= 0; i--){
                if(wordSplit[i].contains("#")){
                    Toast.makeText(getApplicationContext(), wordSplit[i], Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }
    });

答案 3 :(得分:-1)

试试这个:可以帮到你

.row>.col-xs-6*2>p.img*2>img[src="gallery-$.jpg"]