android如何在xmpp中的edittext中分隔spanned对象?

时间:2014-05-14 13:21:03

标签: android xmpp

我正在制作一个聊天应用程序,其中我正在使用表情符号功能。我的表情符号功能对于单个图像正常工作,但是当我拍摄多个情感图像时,它不会转换为特定图像..,仅在某个时间单个图像正在转换,我的问题是

  1. 我无法在编辑文本字段中分隔跨区对象..,对于单个值,它正在工作但是对于多个值,它不起作用..
  2. 示例。我正在编辑文本字段中拍摄4个不同的图像,如下所示

    enter image description here

    enter image description here 现在我想分开它的跨物体。我怎么能这样做 这是代码

     public void keyClickedIndex( final String index) 
    {
    
        ImageGetter imageGetter = new ImageGetter() 
        {
            public Drawable getDrawable(String source) 
            {    
                StringTokenizer st = new StringTokenizer(index, ".");
                Drawable d = new BitmapDrawable(getResources(),emoticons[Integer.parseInt(st.nextToken()) - 1]);
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                return d;
            }
        };
    
    
        Spanned cs = Html.fromHtml("<img src ='"+ index +"'/>", imageGetter, null);        
        int cursorPosition = mSendText.getSelectionStart();     
            mSendText.getText().insert(cursorPosition, cs);
    

    请帮助我..,提前致谢。

1 个答案:

答案 0 :(得分:1)

你可以使用表情符号处理程序方法

private static class EmoticonHandler implements TextWatcher {

    private final EditText mEditor;
    private final ArrayList<ImageSpan> mEmoticonsToRemove = new ArrayList<ImageSpan>();
    //public String txt;
    XMPPClient act;
    public EmoticonHandler(EditText editor,XMPPClient act) {
        // Attach the handler to listen for text changes.
        mEditor = editor;
        mEditor.addTextChangedListener(this);
        this.act = act;
    }

    public void insert(String emoticon, int resource) 
    {
        // Create the ImageSpan
        Drawable drawable = mEditor.getResources().getDrawable(resource);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        ImageSpan span = new ImageSpan(drawable,emoticon,ImageSpan.ALIGN_BASELINE);

        // Get the selected text.
        int start = mEditor.getSelectionStart();
        int end = mEditor.getSelectionEnd();
        Editable message = mEditor.getEditableText();

        // Insert the emoticon.
        message.replace(start, end, emoticon);
        message.setSpan(span, start, start + emoticon.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    @Override
    public void beforeTextChanged(CharSequence text, int start, int count, int after) {
        // Check if some text will be removed.
        if (count > 0) {
            int end = start + count;
            Editable message = mEditor.getEditableText();
            ImageSpan[] list = message.getSpans(start, end, ImageSpan.class);

            boolean check = false;

            for (ImageSpan span : list)
            {
                // Get only the emoticons that are inside of the changed
                // region.

                check = true;
                int spanStart = message.getSpanStart(span);
                int spanEnd = message.getSpanEnd(span);
                //txt = text.toString();
                act.emorTxt =  text.toString();
                if ((spanStart < end) && (spanEnd > start)) {
                    // Add to remove list
                    mEmoticonsToRemove.add(span);
                }
            }

            if(!check)
            {
                act.emorTxt =  text.toString();
            }
        }
    }

    @Override
    public void afterTextChanged(Editable text) {
        Editable message = mEditor.getEditableText();

        // Commit the emoticons to be removed.
        for (ImageSpan span : mEmoticonsToRemove) 
        {
            int start = message.getSpanStart(span);
            int end = message.getSpanEnd(span);

            // Remove the span
            message.removeSpan(span);

            // Remove the remaining emoticon text.
            if (start != end) {
                message.delete(start, end);
            }
        }
        mEmoticonsToRemove.clear();


    }

    @Override
    public void onTextChanged(CharSequence text, int start, int before, int count) {
    }

}

它将完美地工作.... :)