我怎么能在android中刷新我的Spannable?

时间:2013-12-19 01:16:56

标签: java android syntax-highlighting

我希望能够刷新突出显示文本的Spannable,以便每次输入列出的其中一个短语时,它都会突出显示它。但我的问题是,它只能输入一次短语,我一直试图寻找其他方法来刷新它或突出显示它,然后删除它已被突出显示但我迄今为止没有尝试过的任何工作。从我能想到的是,用于突出显示与列出的短语匹配的每个单词的代码是通过执行某些操作 onTextChanged 。但我看不清楚。

editText.addTextChangedListener(new TextWatcher() {
        //Registered functions
        final String FUNCTION = "function";
        final String ENTITY = "Entity";
        final String PLAYER = "Player";
        final String MODPE = "ModPE";
        final String ZERO = "0";
        final String ONE = "1";
        final String TWO = "2";
        final String THREE = "3";
        final String FOUR = "4";
        final String FIVE = "5";
        final String SIX = "6";
        final String SEVEN = "7";
        final String EIGHT = "8";
        final String NINE = "9";
        final String CPER = "{}";
        final String PER = "()";

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int after){

        }
        //Code for Syntax Highlighting
        @Override
        public void afterTextChanged(Editable s) {
            //Registered function (to find beginning text)
            int iFUNCTION = s.toString().indexOf(FUNCTION);
            int iENTITY = s.toString().indexOf(ENTITY);
            int iPLAYER = s.toString().indexOf(PLAYER);
            int iMODPE = s.toString().indexOf(MODPE);
            int iZERO = s.toString().indexOf(ZERO);
            int iONE = s.toString().indexOf(ONE);
            int iTWO = s.toString().indexOf(TWO);
            int iTHREE= s.toString().indexOf(THREE);
            int iFOUR = s.toString().indexOf(FOUR);
            int iFIVE = s.toString().indexOf(FIVE);
            int iSIX = s.toString().indexOf(SIX);
            int iSEVEN = s.toString().indexOf(SEVEN);
            int iEIGHT = s.toString().indexOf(EIGHT);
            int iNINE = s.toString().indexOf(NINE);
            int iCPER = s.toString().indexOf(CPER);
            int iPER = s.toString().indexOf(PER);

            if (iFUNCTION >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.CYAN), iFUNCTION, iFUNCTION + FUNCTION.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iENTITY >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.CYAN), iENTITY, iENTITY + ENTITY.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iPLAYER >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.CYAN), iPLAYER, iPLAYER + PLAYER.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iMODPE >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.CYAN), iMODPE, iMODPE + MODPE.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iZERO >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iZERO, iZERO + ZERO.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iONE >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iONE, iONE + ONE.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iTWO >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iTWO, iTWO + TWO.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iTHREE >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iTHREE, iTHREE + THREE.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iFOUR >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iFOUR, iFOUR + FOUR.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iFIVE >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iFIVE, iFIVE + FIVE.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iSIX >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iSIX, iSIX + SIX.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iSEVEN >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iSEVEN, iSEVEN + SEVEN.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iEIGHT >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iEIGHT, iEIGHT + EIGHT.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iNINE >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.RED), iNINE, iNINE + NINE.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iPER >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.CYAN), iPER, iPER + PER.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (iCPER >= 0) {
                s.setSpan(new ForegroundColorSpan(Color.CYAN), iCPER, iCPER + CPER.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

对于每一项检查,您都可以进行迭代,直到您已经用完所有相同类型的案例为止:

例如:

...
String string = s.toString();
int lastMatch = 0;
while(lastMatch < string.length() && (iFUNCTION = string.indexOf(FUNCTION, lastMatch)) >= 0){
    s.setSpan(new ForegroundColorSpan(Color.CYAN), iFUNCTION, iFUNCTION + FUNCTION.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    lastMatch = iFUNCTION + FUNCTION.length(); // add the length since we've already matched it
}
...

确保将lastMatch重置为0,然后再将其用于下一种类型。

显然,这也将是非常低效的,因为它将为已经具有跨度的区域重新创建跨度,如果已经存在,则可以实现检查以不重新创建跨距,或者仅在已编辑的文本的区域中创建跨度。