我想在每次更改文本时突出显示我的 MutiAutoCompleteTextView 的语法
I want to highlight the syntax of my MutiAutoCompleteTextView for each time text is changed
**我想在每次更改文本时突出显示 MutiAutoCompleteTextView 的语法,我试过这个但我的应用程序在 运行 时冻结,请帮助我如何做。 **
MultiAutoCompleteTextView Code;
TextHighlighter Highlighter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Code = findViewById(R.id.Code);
Highlighter = new TextHighlighter();
Highlighter.setLanguage(Highlighter.JAVA);
String text=Highlighter.getHighlightedText(Code.getText().toString());
Code.setText(Html.fromHtml(text));
Code.addTextChangedListener(TextWatcher);
}
private TextWatcher TextWatcher = new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void afterTextChanged(Editable p1)
{
if(Code.length() !=0)
{
String text=Highlighter.getHighlightedText(Code.getText().toString());
Code.setText(Html.fromHtml(text));
}
}
};
}
您的应用程序冻结,因为当您使用 Code.setText(Html.fromHtml(text));
设置文本时,您的 afterTextChanged
将立即被调用。所以它将 运行 视为无限循环。您可以使用标志控制此流程。
例如:
boolean isHighlighted = false;
private TextWatcher TextWatcher = new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
}
@Override
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void afterTextChanged(Editable p1)
{
String text=Highlighter.getHighlightedText(Code.getText().toString());
if(Code.length() !=0 && !isHighlighted)
{
isHighlighted == true;
Code.setText(Html.fromHtml(text));
//setText will resets cursor position, so you have to move the cursor to the end.
Code.setSelection(text.length)
} else {
isHighlighted = false;
}
}
};
**我想在每次更改文本时突出显示 MutiAutoCompleteTextView 的语法,我试过这个但我的应用程序在 运行 时冻结,请帮助我如何做。 **
MultiAutoCompleteTextView Code;
TextHighlighter Highlighter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Code = findViewById(R.id.Code);
Highlighter = new TextHighlighter();
Highlighter.setLanguage(Highlighter.JAVA);
String text=Highlighter.getHighlightedText(Code.getText().toString());
Code.setText(Html.fromHtml(text));
Code.addTextChangedListener(TextWatcher);
}
private TextWatcher TextWatcher = new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void afterTextChanged(Editable p1)
{
if(Code.length() !=0)
{
String text=Highlighter.getHighlightedText(Code.getText().toString());
Code.setText(Html.fromHtml(text));
}
}
};
}
您的应用程序冻结,因为当您使用 Code.setText(Html.fromHtml(text));
设置文本时,您的 afterTextChanged
将立即被调用。所以它将 运行 视为无限循环。您可以使用标志控制此流程。
例如:
boolean isHighlighted = false;
private TextWatcher TextWatcher = new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
}
@Override
public void onTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
@Override
public void afterTextChanged(Editable p1)
{
String text=Highlighter.getHighlightedText(Code.getText().toString());
if(Code.length() !=0 && !isHighlighted)
{
isHighlighted == true;
Code.setText(Html.fromHtml(text));
//setText will resets cursor position, so you have to move the cursor to the end.
Code.setSelection(text.length)
} else {
isHighlighted = false;
}
}
};