数分の書き込み後、アルゴリズムは、書かれたテキスト全体を制御して強調表示する語句を探し続けているため、実際には遅くなります。問題は、文字が書き込まれるたびにテキストをチェックすることです。私の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() ')を単に開始またはリセットしています。私は一般的な "OK、ユーザーが一時的に入力を停止した"遅延期間として使用さ500msを見てきました。 RxJava/RxAndroidを使用するように切り替えると、「debounce」がIIRCのためにこれを処理できます。 – CommonsWare
ありがとう@コモンズウェア、私はあなたの解決策を試してみましょう! – Adriano