Android TextWatcher OnTextChanged持续启动
我有一个textwatcher,每当按下空格键时,我都会尝试执行一些操作。但我想确保在执行该操作后,我删除了自定义MultiAutoCompleteTextView控件中的空格 因此,我使用了在afterTextChanged事件中编写的代码来替换所有空格。但这会导致一次又一次地调用ContextChanged事件,直到出现StackOverflow运行时错误 我不知道是什么引起了这个问题。请帮帮我Android TextWatcher OnTextChanged持续启动,android,replace,custom-controls,textwatcher,Android,Replace,Custom Controls,Textwatcher,我有一个textwatcher,每当按下空格键时,我都会尝试执行一些操作。但我想确保在执行该操作后,我删除了自定义MultiAutoCompleteTextView控件中的空格 因此,我使用了在afterTextChanged事件中编写的代码来替换所有空格。但这会导致一次又一次地调用ContextChanged事件,直到出现StackOverflow运行时错误 我不知道是什么引起了这个问题。请帮帮我 public class ChipsMultiAutoCompleteTextview exte
public class ChipsMultiAutoCompleteTextview extends MultiAutoCompleteTextView implements OnItemClickListener {
private TextWatcher textWather = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count >=1)
{
if(s.charAt(start) == ' ')
{
setChips(getText().toString()); // generate chips
updateInstruction(getText().toString(), false);
}
else
{
updateInstruction(getText().toString(), true);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void afterTextChanged(final Editable s) {
String i = getText().toString().replaceAll("\\s","");
setText(i);
setSelection(i.length());
}
};
public void setChips(String s){
if(s.contains(" ") && !s.trim().equals("")) // check space in string
{
this.chips = s.trim().split(" ");
SpannableStringBuilder ssb = new SpannableStringBuilder(getText());
// split string wich comma
int x =0;
// loop will generate ImageSpan for every country name separated by comma
for(String c : chips){
// inflate chips_edittext layout
LayoutInflater lf = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) lf.inflate(R.layout.chips_edittext, null);
textView.setText(c); // set text
setFlags(textView, c); // set flag image
// capture bitmapt of genreated textview
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
textView.destroyDrawingCache(); // destory drawable
// create bitmap drawable for imagespan
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
// create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable),x ,x + c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x = x+ c.length() +1;
}
// set chips span
setText(ssb);
// move cursor to last
setSelection(s.length());
}
}
public void updateInstruction(String s,boolean subtract) {
String chipArray[] = s.trim().split(" ");
int len;
if(subtract)
len = chipArray.length-1;
else
len = chipArray.length;
int left = MINIMUM_REQUIRED_TAGS-len; // chips required
if(left<=0)
{
this.instruction.setText("");
proceed = true;
}
else
{
proceed = false;
this.instruction.setText(String.valueOf("Add atleast "+left+" topics..."));
}
}
}
尝试以下解决方案:
textView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// identify key pressed by checking keyCode value with KeyEvent.KEYCODE
if (keyCode == KeyEvent.KEYCODE_SPACE) {
// this is for backspace
}
return false;
}
});
如果不是这样,请尝试参考此帖子,其中可能包含针对您的自动完成行为的解决方案:
嗯,问题的原因似乎是您可能正在更新实际的EditText/TextView,而textwatcher实际上正在监视更改,并且,当您通过textwatcher以编程方式更改该信息时,不可避免地会一次又一次地触发同一事件,这可能会导致该问题。看起来可能有一种不同的方法来满足你的实际需要。@Ernanijopper如果你是对的,那就是正在发生的事情。我试图通过在控件上使用onKeyListener并重写onKeyDown来实现这一点,但这些事件似乎不起作用。@EmaniJoppert我已经尝试过解决方案,事件不会在每次点击键时调用。您是否尝试过这两种解决方案,包括下面的代码和链接?是的,每当我以编程方式替换空格时,我都会进行变通,我设置了一个布尔值,下次调用textchanged事件时,我直接从基于该布尔值的函数返回。这就解决了问题。但是仍然不知道为什么keyevent没有调用类中的side。@EmaniJoppert也在链接中使用了解决方案,您的代码也使用了解决方案。谢谢你的帮助。它起作用了吗?还是你还在坚持?让我们实现吧,伙计