How do I get MultiAutoCompleteTextView tokenizer similar to Facebook app?

左心房为你撑大大i 提交于 2019-11-28 11:11:14

Found the solution....

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

    }
};

Try adding a TextWatcher to the MultiAutoCompleteTextView. Save the current text and check if the last space was deleted. If so, remove the last token.

VijayRaj
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) {
            for(int i=0;i<contentArray.size();i++){
                JSONObject jo=contentArray.get(i);
                try {
                    int keyValue=jo.getInt("startIndx");//No i18N                                                                               
                    if(keyValue==buffer.getSpanStart(link[0])){                                                 
                           jo.put("isRemoved", true);   
                           contentArray.set(i,jo);                                              
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             }
            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;
           for(int i=0;i<contentArray.size();i++){
                JSONObject jo=contentArray.get(i);
                try {
                    int keyValue=jo.getInt("startIndx");//No i18N                                                                               
                    if(keyValue==start){                                
                           jo.put("isRemoved", true);   
                           contentArray.set(i,jo);                                              
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
           if(buffer.charAt(start)==' '){                                       
              buffer.replace(start,start+1,"");
           }
        }
    }              
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!