在输入具有文件扩展名的“编辑”文本或MultiAutoCompleteTextView时更改特定的单词颜色

时间:2018-11-28 04:03:06

标签: java android android-edittext multiautocompletetextview

我创建了一个小型代码编辑器应用程序,问题是,更改范围不会更改特定单词的颜色。我使用字符串数组资源文件来识别特定单词。并添加“通用”类以使用“另存为”对话框来扩展。在普通班上,我在下面使用时遇到了问题,

public static String currentExtention;

空点异常错误。然后我为它设置一个值,例如“ txt”。 但是现在的问题是,当我更改扩展范围时颜色不会改变。 我的课在下面。

public class Common {
    public static String currentExtention="txt";
    public static void setCurrentExtention(String extention)
    {
       currentExtention=extention;
    }

    public static String getCurrentExtention()
    {
       return currentExtention;
    }
} 

我使用FileSaveDialog类更改数据类型。使用

Common.setCurrentExtention(extention );

扩展是,设置值由用户获取。它是工作并通过主要活动来纠正扩展。 在我的“活动”类下面

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_file);

    txtnumberView = (TextView) findViewById(R.id.numberViewText);
    edtTextView = (MultiAutoCompleteTextView) findViewById(R.id.edtTextView);
    relativeLayout = (RelativeLayout) findViewById(R.id.layout_root);
    helper = new TextViewUndoRedo(edtTextView);
    context = NewFileActivity.this;
    fileSaveDialog = new FileSaveDialog(context);
    findTextDialog = new FindTextDialog(context);
    autoCompleteText = new AutoCompleteText(context);
    autoChangeNumberTxtView=new AutoChangeNumberTxtView(context,edtTextView,txtnumberView);

     switch (Common.getCurrentExtention()) {
        case "html":
            dataType = getResources().getStringArray(R.array.html);
            break;
        case "txt":
            dataType = getResources().getStringArray(R.array.txt);
            break;
    }
    regex = new StringBuilder("\\b(");
    for (String word : dataType) {
        regex.append(Pattern.quote(word));
        regex.append("|");
    }
    regex.setLength(regex.length() - 1); // delete last added "|"
    regex.append(")\\b");
    edtTextView.addTextChangedListener(new TextWatcher() {
        ColorScheme keywords = new ColorScheme(

                Pattern.compile(regex.toString()),
                Color.CYAN
        );

        ColorScheme numbers = new ColorScheme(
                Pattern.compile("(\\b(\\d*[.]?\\d+)\\b)"),
                Color.BLUE
        );

        final ColorScheme[] schemes = {keywords, numbers};

        void removeSpans(Editable e, Class<? extends CharacterStyle> type) {
            CharacterStyle[] spans = e.getSpans(0, e.length(), type);
            for (CharacterStyle span : spans) {
                e.removeSpan(span);
            }
        }

        class ColorScheme {
            final Pattern pattern;
            final int color;

            ColorScheme(Pattern pattern, int color) {
                this.pattern = pattern;
                this.color = color;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            autoChangeNumberTxtView.autoOnchange();
            textInType();


        }

        @Override
        public void afterTextChanged(Editable editable) {

                removeSpans(editable, ForegroundColorSpan.class);
                for (ColorScheme scheme : schemes) {
                    for (Matcher m = scheme.pattern.matcher(editable); m.find(); ) {
                        editable.setSpan(new ForegroundColorSpan(scheme.color),
                                m.start(),
                                m.end(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }


            }
        }

    });

对于我的进一步开发,我使用MultiAutoCompleteTextView而不是EditText。 我认为那里有问题。

  switch (Common.getCurrentExtention()) {
        case "html":
            dataType = getResources().getStringArray(R.array.html);
            break;
        case "txt":
            dataType = getResources().getStringArray(R.array.txt);
            break;
    }
    regex = new StringBuilder("\\b(");
    for (String word : dataType) {
        regex.append(Pattern.quote(word));
        regex.append("|");
    }
    regex.setLength(regex.length() - 1); // delete last added "|"
    regex.append(")\\b");
    edtTextView.addTextChangedListener(new TextWatcher() {
        ColorScheme keywords = new ColorScheme(

                Pattern.compile(regex.toString()),
                Color.CYAN
        );

        ColorScheme numbers = new ColorScheme(
                Pattern.compile("(\\b(\\d*[.]?\\d+)\\b)"),
                Color.BLUE
        );

        final ColorScheme[] schemes = {keywords, numbers};

        void removeSpans(Editable e, Class<? extends CharacterStyle> type) {
            CharacterStyle[] spans = e.getSpans(0, e.length(), type);
            for (CharacterStyle span : spans) {
                e.removeSpan(span);
            }
        }

        class ColorScheme {
            final Pattern pattern;
            final int color;

            ColorScheme(Pattern pattern, int color) {
                this.pattern = pattern;
                this.color = color;
            }
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {

                removeSpans(editable, ForegroundColorSpan.class);
                for (ColorScheme scheme : schemes) {
                    for (Matcher m = scheme.pattern.matcher(editable); m.find(); ) {
                        editable.setSpan(new ForegroundColorSpan(scheme.color),
                                m.start(),
                                m.end(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

这不是崩溃,但下面显示了logcat。当我更改文件扩展名时。

E/ViewRootImpl: sendUserActionEvent() mView == null
E/ViewRootImpl: sendUserActionEvent() mView == null

如果有人可以向我解释问题所在或正确的解决方法,这将对我非常有帮助。

0 个答案:

没有答案