久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

帶有 SpannableStringBuilder 和 ImageSpan 的 EditText 不能

EditText with SpannableStringBuilder and ImageSpan doesn#39;t works fine(帶有 SpannableStringBuilder 和 ImageSpan 的 EditText 不能正常工作)
本文介紹了帶有 SpannableStringBuilder 和 ImageSpan 的 EditText 不能正常工作的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試將表情符號放入 EditText.我已經(jīng)設(shè)法做到了,并且效果很好,但是當(dāng)我嘗試使用軟鍵盤從 EditText 中刪除這些表情符號時遇到了問題.我無法通過單擊刪除按鈕來執(zhí)行此操作.當(dāng)我插入一個新的 ImageSpan 時,我為它替換了一個 imageId,但是當(dāng)我嘗試刪除 de icon 時,我必須在刪除圖像之前刪除所有 imageId 字符.

I'm trying to put emoticons inside a EditText. I've managed to do it and it works fine but I have a problem when I try to delete these emoticons from the EditText using the soft keyboard. I can't do this action with a single delete button's click. When I insert a new ImageSpan I replace an imageId for it but when I try to delete de icon I have to delete all the imageId characters before delete the image.

String fileName = "emoticon1.png";
Drawable d = new BitmapDrawable(getResources(), fileName);
String imageId = "[" + fileName + "]";
int cursorPosition = content.getSelectionStart();
int end = cursorPosition + imageId.length();
content.getText().insert(cursorPosition, imageId);

SpannableStringBuilder ss = new SpannableStringBuilder(content.getText());
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, cursorPosition, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
content.setText(ss, TextView.BufferType.SPANNABLE);
content.setSelection(end);

我需要通過單擊刪除按鈕來刪除表情符號.請問你能幫幫我嗎?

I need to remove the emoticons with a single delete button's click. Could you help me, please?

謝謝!

推薦答案

這是在 EditText 中處理表情符號的實(shí)現(xiàn).此實(shí)現(xiàn)使用 TextWatcher 來監(jiān)視 EditText 的更改,并檢測刪除某些文本時是否刪除了某些表情符號.

This is the implementation to handle emoticons inside a EditText. This implementation uses the TextWatcher to monitor the EditText changes and detect if some emoticon was removed when some text is deleted.

請注意,此實(shí)現(xiàn)還驗證是否刪除了文本選擇(不僅僅是刪除鍵).

Note that this implementation also verifies if a text selection was deleted (not only the delete key).

為避免在輸入文本時出現(xiàn)文本預(yù)測問題,建議將表情符號文本用空格包圍(文本預(yù)測可以將表情符號文本與相鄰文本連接起來).

To avoid issues with text prediction when typing a text, it is recommended to surround the emoticon text with spaces (the text prediction can join the emoticon text with the adjacent text).

package com.takamori.testapp;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EmoticonHandler mEmoticonHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editor = (EditText) findViewById(R.id.messageEditor);
        // Create the emoticon handler.
        mEmoticonHandler = new EmoticonHandler(editor);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_insert_emoticon:
                // WARNING: The emoticon text shall be surrounded by spaces
                // to avoid issues with text prediction.
                mEmoticonHandler.insert(" :-) ", R.drawable.smile);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private static class EmoticonHandler implements TextWatcher {

        private final EditText mEditor;
        private final ArrayList<ImageSpan> mEmoticonsToRemove = new ArrayList<ImageSpan>();

        public EmoticonHandler(EditText editor) {
            // Attach the handler to listen for text changes.
            mEditor = editor;
            mEditor.addTextChangedListener(this);
        }

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

                for (ImageSpan span : list) {
                    // Get only the emoticons that are inside of the changed
                    // region.
                    int spanStart = message.getSpanStart(span);
                    int spanEnd = message.getSpanEnd(span);
                    if ((spanStart < end) && (spanEnd > start)) {
                        // Add to remove list
                        mEmoticonsToRemove.add(span);
                    }
                }
            }
        }

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

    }
}

這篇關(guān)于帶有 SpannableStringBuilder 和 ImageSpan 的 EditText 不能正常工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 一本大道久久a久久精二百 国产成人免费在线 | 日韩欧美大片 | www.日本精品| www国产成人 | 欧美精品第一页 | 91在线视频精品 | 91av在线免费播放 | 国产毛片毛片 | 国产一区二区三区在线 | 成人福利网| 成人av网站在线观看 | av特级毛片 | 日韩av在线一区二区 | 精品日韩| h在线看 | av香蕉| 久久精品欧美一区二区三区不卡 | 亚洲美女视频 | 狠狠爱视频 | 亚洲一区导航 | 日韩视频一级 | 精品1区| 日韩成人av在线播放 | 天堂成人国产精品一区 | 狠狠综合久久av一区二区老牛 | 一区二区三区中文字幕 | 久久久久亚洲精品 | 天天操网| 蜜桃五月天 | 免费黄色大片 | 久久国产精品99久久久久久丝袜 | 免费视频中文字幕 | 亚洲欧美精品国产一级在线 | 不卡av电影在线播放 | 高清一区二区视频 | 久久中文高清 | 欧美日韩高清一区 | 亚洲天天干 | 国产成人久久精品一区二区三区 | 日本高清中文字幕 | 中文字幕亚洲欧美 |