如何优化语法突出显示?

时间:2016-05-05 17:08:16

标签: android performance syntax-highlighting android-textwatcher

我在Android中制作文本编辑器,我想为其添加语法高亮显示。在我当前的实现中,当我输入内容时,UI是滞后的。我需要帮助来优化我当前的实现。

private void onEditorListener() {

    edtEditor.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            if (timer != null) {
                timer.cancel();
            }


        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(final Editable s) {

            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // do your actual work here
                    MaineActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });

                    try {

                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    MaineActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textHighLighter(s);
                        }
                    });
                }
            }, 600);


        }

    });
}
private void textHighLighter(Editable s) {

    // Check the matcher for general keywords..
    Matcher a = Patterns.GENERAL_KEYWORDS.matcher(s.toString());
    // Check the matcher for html tags..
    Matcher b = Patterns.HTML_TAGS.matcher(s.toString());
    // Check the matcher for html attribute..
    Matcher c = Patterns.HTML_ATTRS.matcher(s.toString());
    // Check the matcher for symbol..
    Matcher d = Patterns.SYMBOLS.matcher(s.toString());
    // Check the matcher for general strings..
    Matcher e = Patterns.GENERAL_STRINGS.matcher(s.toString());
    // Find all the general key words and change the text color...
    // Find all the html tags words and change the text color...
    while (b.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.orchid)), b.start(), b.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (a.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bounded)), a.start(), a.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (c.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.henn)), c.start(), c.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (d.find()) {
        s.setSpan(new ForegroundColorSpan(Color.WHITE), d.start(), d.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

    while (e.find()) {
        s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.general_str)), e.start(), e.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //android.util.Log.i("VINCE", b.toString());
    }

}

2 个答案:

答案 0 :(得分:0)

以下是一些建议:

使用RunnableTimerTask代替textHighLighter(Editable s)。我假设你想在调用afterTextChanged(final Editable s)之后调用handler.postDelayed(Runnable r, long delay)之前等待n毫秒。为此,请使用handler.removeCallbacks(Runnable)Thread.sleep(2000)

删除Editable

CharSequence扩展toString()。请勿在{{1​​}}上致电Editable,只需将其作为参数传递给Pattern#matcher(CharSequence)

答案 1 :(得分:0)

@Jared Rummler here is my code

@Overrideenter code here
            public void afterTextChanged(final Editable s) {
                last_text_edit = System.currentTimeMillis();
                handler.postDelayed(runnable, idle_min);
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (System.currentTimeMillis() >= (last_text_edit + idle_min - 500)) {
                                // user hasn't changed the EditText for longer than
                                // the min delay (with half second buffer window)

                                textHighLighter(s);  // your queries
                                if (!already_queried) { // don't do this stuff twice.
                                    already_queried = true;
                                    //textHighLighter(s);  // your queries
                                    android.util.Log.i("VINCE", "already_queried");
                                }
                            }

                            //already_queried = false;
                            handler.removeCallbacks(runnable);
                            android.util.Log.i("VINCE", "Cancelled");
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                };