我如何获得MultiAutoCompleteTextView标记生成器类似于Facebook应用程序?生成器、类似于、如何获得、应用程序

2023-09-04 09:00:35 作者:竟已思之愈浓

我创建具有收件人字段,就像在Facebook应用程序的新邮件功能的应用程序。

I am creating an application which has a 'To' field just like in Facebook app's "New Message" feature.

从下拉列表中选择一个项目后,我创建一个imagespan并将其添加到 MultiAutoCompleteTextView 。我已经使用 SpaceTokenizer 此视图。问题是,当我点击退格键,光标先移动到空的空间(即空间标记生成器),然后当我再次点击退格,全词得到删除了....我想删除退格我的第一次点击整个单词就像Facebook应用程序...

After selecting an item from the drop down list, I create an imagespan and add it to the MultiAutoCompleteTextView. I have used SpaceTokenizer for this view . The problem is when I click on backspace, the cursor first moves to the empty space (i.e., space Tokenizer) and then when I click on the backspace again, the whole word gets deleted....I want to delete the whole word on my first click of backspace just like facebook app...

下面是我的$ C $下 SpaceTokenizer

Here is my code for SpaceTokenizer

     multiContentText.setTokenizer(new Tokenizer(){
     public int findTokenStart(CharSequence text, int      cursor) {
        int i = cursor;
        if(i>0){
            Log.d("textchar ",""+text.charAt(i - 1));
        }

        while (i > 0 && text.charAt(i - 1) != ' ') {
            i--;
        }
        while (i < cursor && text.charAt(i) == ' ' || text.charAt(i - 1) == '\n') {
            i++;
        }

        return i;
    }

    public int findTokenEnd(CharSequence text, int cursor) {
        int i = cursor;
        int len = text.length();

        while (i < len) {
            if (text.charAt(i) == ' ' || text.charAt(i - 1) == '\n') {
                return i;
            } else {
                i++;
            }
        }

        return len;
    }

    public CharSequence terminateToken(CharSequence text) {
        int i = text.length();
        while (i > 0 && text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n') {
            i--;
        }

        if (i > 0 && text.charAt(i - 1) == ' ' || text.charAt(i - 1) == '\n') {
            return text;
        } else {
            if (text instanceof Spanned) {                               
                SpannableString sp = new SpannableString(text + " ");
                TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                                        Object.class, sp, 0);
                return sp;
            } else {                                 
                return text+" ";
            }
        }
    }
});

我用这code创建一个的TextView 在我的多ContentText

I am using this code to create a TextView in my multi-ContentText

SpannableStringBuilder ssb = new SpannableStringBuilder(multiContentText.getText());
String c="text from the list";
TextView textView = (TextView) inflater.inflate(R.layout.chips_edittext, null);
textView.setText(c); // set text
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),0 ,c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// set chips span 
multiContentText.setText(ssb);      
multiContentText.setSelection(multiContentText.getText().length());

我不知道空间标记生成器是否是正确的选择为这种类型的行为...任何帮助或指针将不胜感激......

I am not sure whether the space Tokenizer is the right option for this type of behavior...Any help or pointers will be grateful...

下面是截图为更好地理解....

Here is the screenshot for better understanding....

我有一个文本后跟一个空格,然后光标...如果我打了退格键,它第一次将移动到空的空间,只有当我再次打退格键,整段文字被删除......

I have a text followed by a space and then a cursor...If I hit backspace, it first moves to the empty space and only when I hit backspace again the whole text is deleted....

下面是另一张截图..

Here is the another screenshot ..

下面光标是不完全在两的TextView 秒之间不像Facebook应用程序而这又导致插入文本的一些问题...

Here the cursor is not exactly in between the two TextViews unlike in facebook app which again causes some issues in inserting the text...

推荐答案

找到了解决办法......

Found the solution....

将此textwatcher到multiautocompletetextview

Add this textwatcher to the multiautocompletetextview

private TextWatcher textWather = new TextWatcher() {
    int noOfCharAdded=0;int noOfCharDeleted=0;
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        startIdx=start;
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        noOfCharAdded=after;
        noOfCharDeleted=count;
    }
    @Override
    public void afterTextChanged(Editable s) {
        Editable buffer = s;
        int start = multiContentText.getSelectionStart()<0?0:multiContentText.getSelectionStart();
        int end = multiContentText.getSelectionEnd()<0?0:multiContentText.getSelectionEnd();
                if(noOfCharAdded==0 && noOfCharDeleted==1){ //if space is deleted
                        if (start == end && delPrevText) {                          
                            ImageSpan link[] = buffer.getSpans(start, end,ImageSpan.class);
                            if (link.length > 0) {                                  
                                buffer.replace(buffer.getSpanStart(link[0]),buffer.getSpanEnd(link[0]),"");
                                buffer.removeSpan(link[0]);
                            }
                        }
                        delPrevText=true; 
                        multiContentText.setSelection(multiContentText.getText().length());
                }
                else if(noOfCharAdded==0 && noOfCharDeleted>1){//if the whole word is deleted
                        if(buffer.length()>0){                                           
                            if(start<buffer.length()){
                               delPrevText=false;                                  
                               if(buffer.charAt(start)==' '){                                       
                                  buffer.replace(start,start+1,"");
                               }
                            }
                        }                      
                }               

    }
};