大家好,我在android的文本编辑器中工作,我想优化我的文本编辑器。当我的文本编辑器有100行代码时,它变得迟钝 这是我的实现代码:
private void onEditorListener() {
edtEditor.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
_lastTypeTime = new Date();
}
@Override
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
}
}
//already_queried = false;
handler.removeCallbacks(runnable);
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
});
}
答案 0 :(得分:0)
试试这个。
在字段变量中添加private boolean blocks = false;
。
private void onEditorListener() {
edtEditor.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
@Override
public void afterTextChanged(final Editable s) {
if (blocks) {
return;
}
blocks = true;
textHighLighter(s); // your queries
blocks = false;
}
}