Nach ein paar Minuten des Schreibens wird es wirklich langsam, weil der Algorithmus den gesamten geschriebenen Text nach Wörtern sucht, die hervorgehoben werden sollen. Das Problem ist, dass der Text jedes Mal überprüft wird, wenn ein Brief geschrieben wird.Meine CodeEditor Android App wird langsam, da der Algorithmus nach Wörtern sucht, die jedes Mal hervorgehoben werden sollen, wenn ein Buchstabe geschrieben wird. Wie kann ich es reparieren?
Wie kann ich diesen Algorithmus ändern, um die App schneller und benutzbarer zu machen?
@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();
}
}
Hier ist die gesamte ActivityMain der App.
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);
}
}
}
Wenn Sie etwas Code aufschreiben könnten, wäre es eine große Hilfe.
Anstatt die Formatierung nach jedem Zeichen zu aktualisieren, aktualisieren Sie die Formatierung nach einer Schreibpause. Ihr 'afterTextChanged()' startet oder setzt lediglich einen Timer zurück (z. B. 'postDelayed()' auf 'TextView'), wo Sie die Formatierung aktualisieren, wenn der Timer abläuft. Ich habe gesehen, 500ms verwendet als eine gemeinsame "OK, der Benutzer hörte auf, kurz zu tippen" Verzögerung. Wenn Sie zu RxJava/RxAndroid wechseln, kann "debounce" dies für Sie IIRC handhaben. – CommonsWare
Danke @CommonsWare, ich werde deine Lösung versuchen! – Adriano