Android Spanned Text while deleting smiles shows text

时光毁灭记忆、已成空白 提交于 2019-12-13 02:54:26

问题


I have SpannedText in EditText and I am replacing text with smiles for example cool replaced with smile. But when I am pressing Backspace on android phone (Nexus 5 kitkat 4.4.4) it shows "(Smile)cool" then it deletes the letters at last the smile itself. But on Genymotion emulator it works as needed. What is wrong with my code?

This code is in for

Drawable d = Drawable.createFromStream(ims, null);
                Bitmap b = ((BitmapDrawable)d).getBitmap();
                int spanSmileHeight = (displayHeight>displayWidth) ? ((int) ((double) displayHeight/25)) : ((int) ((double) displayWidth/25));
                int spanSmileWidth = ((int) ((double) spanSmileHeight/b.getHeight())*b.getWidth())+15;
                Bitmap bitmapResized = Bitmap.createScaledBitmap(b, spanSmileWidth, spanSmileHeight, false);
                Drawable sd = new BitmapDrawable(getResources(), bitmapResized);
                sd.setBounds(0, 0, spanSmileWidth, spanSmileHeight);
                addPattern(emoticons, smileyCode, sd);
...

ImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int start = Math.max(message.getSelectionStart(), 0);
                        int end = Math.max(message.getSelectionEnd(), 0);
                        message.getText().replace(Math.min(start, end), Math.max(start, end),
                                smileyCode, 0, smileyCode.length());
                        message.setText(getSmiledText(view.getContext(), message.getText()));
                        message.setSelection(start + smileyCode.length());

and this

public static boolean addSmiles(Context context, Spannable spannable) {
        boolean hasChanges = false;
        for (Map.Entry<Pattern, Drawable> entry : emoticons.entrySet()) {
            Matcher matcher = entry.getKey().matcher(spannable);
            while (matcher.find()) {
                boolean set = true;
                for (ImageSpan span : spannable.getSpans(matcher.start(),
                        matcher.end(), ImageSpan.class))
                    if (spannable.getSpanStart(span) >= matcher.start()
                            && spannable.getSpanEnd(span) <= matcher.end())
                        spannable.removeSpan(span);
                    else {
                        set = false;
                        break;
                    }
                if (set) {
                    hasChanges = true;
                    spannable.setSpan(new ImageSpan(entry.getValue(), ImageSpan.ALIGN_BOTTOM),
                            matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return hasChanges;
    }

    public static Spannable getSmiledText(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addSmiles(context, spannable);
        return spannable;
    }

Before bacspace

After backspace(of virtual keyobard) pressed

The log output:

09-01 06:22:21.280    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat*
09-01 06:23:32.060    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat
09-01 06:23:32.080    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**
09-01 06:23:32.084    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat

3 more times

09-01 06:24:07.980    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**ca
09-01 06:24:08.264    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**c
09-01 06:24:08.668    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**

And here smile deleted

09-01 06:24:09.248    1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat*

回答1:


I have found a way how to prevent this. If we change targetSDK to 15 then smiles are deleted succesfully.



来源:https://stackoverflow.com/questions/25566116/android-spanned-text-while-deleting-smiles-shows-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!