android 如何在 xmpp 的 edittext 中分离跨越的对象?

标签 android xmpp

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

  1. 我无法在编辑文本字段中分离跨越的对象..,对于单个值它可以工作但对于多个值它不起作用..

示例。我在编辑文本字段中拍摄了 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);

请帮助我..,提前致谢

最佳答案

可以使用表情处理方法

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

}

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

关于android 如何在 xmpp 的 edittext 中分离跨越的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23655939/

相关文章:

java - 如何找到两个纬度和经度之间的角度并将其显示在圆上?

android - 构建Android XMPP即时通讯工具和服务器示例

android - Parse 和 Xmpp 同时登录与 facebook 集成

android - XMPP好友列表显示

Javascript 无法在移动设备上正确触发 - iOS/Android

android - Xamarin.Forms 和 FCM 实现不起作用

ios - XMPPFramework - 如何创建群聊室?

android - 在 Windows 上构建 asmack

java - 找不到从方法 android java.lang.NoClassDefFoundError 引用的类

android - 将 fragment 之间的数据传递给 Activity