0
写了几分钟后,它变得非常慢,因为该算法持续控制整个书写文本以寻找单词来突出显示。问题在于,每次写入信件时都会检查文本。我的CodeEditor Android应用程序变慢,因为每次写入字母时,算法都会检查单词以突出显示。我该如何解决它?
如何更改此算法以使应用程序更快更好用?
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String[] words = input.split(SPACE);
int start = 0;
for(int i=0;i<words.length ;i++){
String word = words[i];
if(map.containsKey(word)){
int index = input.indexOf(word, start);
String color = map.get(word);
s.setSpan(new ForegroundColorSpan(Color.parseColor(color)), index, index+word.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start = index+word.length();
}
}
这是应用程序的整个ActivityMain。
package com.example.android.editor;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import java.util.HashMap;
import java.util.Map;
import static com.example.android.editor.R.id.editText;
/**
* ------------
* 30/03/2017
* ------------
* */
public class MainActivity extends Activity {
String projectTitle;
//send code via social Networks
public void invia(View view) {
String shareBody = "File: " + editTextTitle.getText().toString() + "\n\n" +editTextBody.getText().toString();
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.app_name)));
}
public void save(View view) {
String title = editTextTitle.getText().toString();
String content = editTextBody.getText().toString();
pref.setPrefs(pref.sharedPrefsProjects);
startActivity(new Intent(this, SelectProject.class));
if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
if(!TextUtils.isEmpty(projectTitle)) {
pref.removeValue(content);
pref.saveStringValue(title, content);
if(!projectTitle.equals(title)) {
pref.removeValue(projectTitle);
pref.removeValue(content);//adri
pref.saveStringValue(title, content);
}
} else {
pref.removeValue(content);//adri
pref.saveStringValue(title, content);
}
startActivity(new Intent(this, SelectProject.class));
}
}
//This function make possible to insert tab
public void tab(View view) {
editTextBody.getText().insert(editTextBody.getSelectionStart(), "\t\t\t\t");
}
Map<String, String> map = new HashMap<String, String>();
EditText editTextTitle, editTextBody;
SharedPref pref;
private final static String SPACE = "\\s";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTitle = (EditText)findViewById(R.id.titolo);
editTextBody = (EditText)findViewById(editText);
pref = new SharedPref(this);
//dictionary of words to be highlighted and colors
map.put("if", "#00E676");
map.put("else", "#00E676");
map.put("for", "#00E676");
map.put("while", "#00E676");
map.put("do", "#00E676");
map.put("int", "#e91e63");
map.put("void", "#e91e63");
map.put("sizeof", "#e91e63");
map.put("char", "#e91e63");
map.put("bool", "#e91e63");
map.put("float", "#e91e63");
map.put("double", "#e91e63");
map.put("unsigned", "#e91e63");
map.put("+", "#e91e63");
map.put("-", "#e91e63");
map.put("=", "#e91e63");
map.put("false", "#2196F3");
map.put("true", "#2196F3");
map.put("main", "#2196F3");
map.put("<stdio.h", "#2196F3");
map.put("<stdlib.h", "#2196F3");
map.put("printf", "#2196F3");
map.put("scanf", "#2196F3");
map.put("alloc", "#2196F3");
map.put("malloc", "#2196F3");
map.put("realloc", "#2196F3");
map.put("%c", "#673AB7");
map.put("%d", "#673AB7");
map.put("%f", "#673AB7");
map.put("(", "#FFEB3B");
map.put(")", "#FFEB3B");
map.put("(){", "#FFEB3B");
map.put("()", "#FFEB3B");
map.put("{", "#FFEB3B");
map.put("}", "#FFEB3B");
map.put("1", "#7C4DFF");
map.put("2", "#7C4DFF");
map.put("3", "#7C4DFF");
map.put("4", "#7C4DFF");
map.put("5", "#7C4DFF");
map.put("6", "#7C4DFF");
map.put("7", "#7C4DFF");
map.put("8", "#7C4DFF");
map.put("9", "#7C4DFF");
map.put("0", "#7C4DFF");
map.put("*", "#FFFFFF");
editTextBody.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String[] words = input.split(SPACE);
//FUNCTION THAT HIGHLIGHT WORDS
//HERE IS THE PROBLEM
int start = 0;
for(int i=0;i<words.length ;i++){
String word = words[i];
if(map.containsKey(word)){
int index = input.indexOf(word, start);
String color = map.get(word);
s.setSpan(new ForegroundColorSpan(Color.parseColor(color)), index, index+word.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start = index+word.length();
}
}
}
});
if(getIntent() != null) {
projectTitle = getIntent().getStringExtra("title");
String projectBody = getIntent().getStringExtra("body");
editTextBody.setText(projectBody);
editTextTitle.setText(projectTitle);
}
}
}
如果你可以写下一些代码,这将是一个很大的帮助。
与其在每个字符后更新格式,不如在键入时暂停后更新格式。你的'afterTextChanged()'只是启动或重置一个定时器(例如'TextView'上的'postDelayed()'),当定时器关闭时更新格式。我已经看到500ms作为一个普通的“确定,用户暂时停止输入”延迟期。如果你切换到使用RxJava/RxAndroid,“debounce”可以为你处理这个IIRC。 – CommonsWare
谢谢@CommonsWare,我会试试你的解决方案! – Adriano