Android and the CommaTokenizer

两盒软妹~` 提交于 2019-12-05 05:43:34

问题


I need a Tokenizer (for the AutoCompleteTextview) which can do the following:

  1. Two words must be recognized as such when separated by a blank character
  2. Two words must also be recognized as such when separated by a newline ("Enter" pressed)

1) is working, but how can I accomplish 2?

public class SpaceTokenizer implements Tokenizer {

@Override
public int findTokenStart(CharSequence text, int cursor) {
    int i = cursor; 
    while (i > 0 && (text.charAt(i - 1) != ' ')) {
        i--;
    }
    while (i < cursor && (text.charAt(i) == ' ' || text.charAt(i) == '\n')) {
        i++;
    }   
    return i;
}

@Override
public int findTokenEnd(CharSequence text, int cursor) {
    int i = cursor;
    int len = text.length();

    while (i < len) {
        if (text.charAt(i) == ' ' || text.charAt(i) == '\n') {
            return i;
        } else {
            i++;
        }
    }   
    return len;
}

@Override
public CharSequence terminateToken(CharSequence text) {
    int i = text.length();

    while (i > 0 && (text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n')) {
        i--;
    }   
    if (i > 0 && (text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n')) {
        return text;
    } else {
        if (text instanceof Spanned) {
            SpannableString sp = new SpannableString(text + " ");
            TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                    Object.class, sp, 0);
            return sp;
        } else {
            return text + " ";
        }
    }
}
}

回答1:


You should be able to do this: text.charAt(i) == ' ' || text.charAt(i) == '\n' or i-1 where appropriate.




回答2:


Here is the whole code for anyone that is interested, had to implement this also in my own app. Thanks to answer from @Haphazard.

@Override
public int findTokenStart(CharSequence text, int cursor) {
    int i = cursor; 
    while (i > 0 && (text.charAt(i - 1) != ' ' && text.charAt(i - 1) != '\n')) {
        i--;
    }
    while (i < cursor && (text.charAt(i) == ' ' || text.charAt(i) == '\n')) {
        i++;
    }   
    return i;
}

@Override
public int findTokenEnd(CharSequence text, int cursor) {
    int i = cursor;
    int len = text.length();

    while (i < len) {
        if (text.charAt(i) == ' ' || text.charAt(i) == '\n') {
            return i;
        } else {
            i++;
        }
    }   
    return len;
}

@Override
public CharSequence terminateToken(CharSequence text) {
    int i = text.length();

    while (i > 0 && (text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n')) {
        i--;
    }   
    if (i > 0 && (text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n')) {
        return text;
    } else {
        if (text instanceof Spanned) {
            SpannableString sp = new SpannableString(text + " ");
            TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                    Object.class, sp, 0);
            return sp;
        } else {
            return text + " ";
        }
    }
}


来源:https://stackoverflow.com/questions/5811336/android-and-the-commatokenizer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!