TextWatcher OnTextChanged继续射击

时间:2014-08-09 21:09:36

标签: android replace custom-controls textwatcher

我是一个textwatcher,我试图在按下space key时执行某些操作。但我想确保在执行操作后删除自定义MultiAutoCompleteTextView控件中的空格。

所以我使用afterTextChanged事件中编写的代码来替换所有空格。但是这导致onTextChanged事件被一次又一次地调用,直到我得到StackOverflow运行时错误。

我不知道造成问题的原因。请帮助我。

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..."));
    }
}
}

1 个答案:

答案 0 :(得分:0)

试试这个解决方案:

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;
    }
});

如果不是这种情况,请尝试参考此帖子,其中可能包含自动填充行为的解决方案:

Ignore spaces while searching listview android