Android EditText中插入图片并响应点击事件

EditText中插入图片基本就是两种方法:

1,通过Html.fromHtml(..)来实现
     [mw_shl_code=java,true]eText.append(Html.fromHtml("<img src=‘" + R.drawable.ohoh + "‘/>",
                                                imageGetter, null));[/mw_shl_code]
2,通过ImageSpan与SpannableString/SpannableStringBuilder.
[mw_shl_code=java,true]SpannableString ss = new SpannableString(imgStr);
                                Drawable drawable = getResources().getDrawable(
                                                R.drawable.bad_smelly);
                                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                                                drawable.getIntrinsicHeight());
                                ImageSpan imageSpan = new ImageSpan(drawable,
                                                ImageSpan.ALIGN_BASELINE);
                                ss.setSpan(imageSpan, 0, imgStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                eText.append(ss);

下面贴上完整代码,
点击事件通过增加ClickableSpan来实现。

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Html.ImageGetter;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
        private final String TAG = "MainActivity";
        private EditText eText;
        private int spanEnd;
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                eText = (EditText) findViewById(R.id.test1_editText1);
                final String imgStr = "[smile]";
               
//                eText.setOnClickListener(textListener); //用textListener实现ImageSpan响应点击不靠谱,有时无效。
               
                final ImageGetter imageGetter = new ImageGetter() {
                        @Override
                        public Drawable getDrawable(String source) {
                                int id = Integer.parseInt(source);

                                // 根据id从资源文件中获取图片对象
                                Drawable d = getResources().getDrawable(id);
                                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                                return d;
                        }
                };

                Button button = (Button) findViewById(R.id.test1_button1);
                Button button2 = (Button) findViewById(R.id.test1_button2);
                Button button3 = (Button) findViewById(R.id.test1_button3);
               
                button.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                SpannableString ss = new SpannableString(imgStr);
                                Drawable drawable = getResources().getDrawable(
                                                R.drawable.hungry_1);
                                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                                                drawable.getIntrinsicHeight());
                                ImageSpan imageSpan = new ImageSpan(drawable,
                                                ImageSpan.ALIGN_BASELINE);
                                ss.setSpan(imageSpan, 0, imgStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                                int nowLocation = eText.getSelectionStart();
                                //需要对nowLocation进行判断以免出现越界异常。
                                eText.getText().insert(nowLocation, ss);
                                setSpanClickable();
                                eText.getText().insert(spanEnd , "\n");
                        }
                });
               
                button2.setOnClickListener(new OnClickListener(){
                        public void onClick(View v) {
                                SpannableString ss = new SpannableString(imgStr);
                                Drawable drawable = getResources().getDrawable(
                                                R.drawable.bad_smelly);
                                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                                                drawable.getIntrinsicHeight());
                                ImageSpan imageSpan = new ImageSpan(drawable,
                                                ImageSpan.ALIGN_BASELINE);
                                ss.setSpan(imageSpan, 0, imgStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                eText.append(ss);
                                setSpanClickable();
                                eText.append("\n");
                        }
                        
                });
               
                button3.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                eText.append(Html.fromHtml("<img src=‘" + R.drawable.ohoh + "‘/>",
                                                imageGetter, null));
                                setSpanClickable();
                        }
                });

        }
        
        
        private OnClickListener textListener = new OnClickListener() {
                //此方法不靠谱
                @Override
                public void onClick(View v) {
                        // 关闭软键盘
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(eText.getWindowToken(), 0);
                        
                        Spanned s = eText.getText();
                        ImageSpan[] imageSpans = s.getSpans(0, s.length(), ImageSpan.class);
                        int selectionStart = eText.getSelectionStart();
                        for (ImageSpan span : imageSpans) {
                int start = s.getSpanStart(span);
                int end = s.getSpanEnd(span);
               
                                if (selectionStart >= start && selectionStart < end)// 找到图片
                                {
                                        Toast.makeText(getApplicationContext(), "找到图片", Toast.LENGTH_SHORT).show();
                                        return;
                                }
                        }
                        
                        // 打开软键盘
                        imm.showSoftInput(eText, 0);
                }
        };
        
        
        public void setSpanClickable() {
                //此方法比较靠谱
                Spanned s = eText.getText();
                //setMovementMethod很重要,不然ClickableSpan无法获取点击事件。
                eText.setMovementMethod(LinkMovementMethod.getInstance());
                ImageSpan[] imageSpans = s.getSpans(0, s.length(), ImageSpan.class);
               
                for (ImageSpan span : imageSpans) {
                        final String image_src = span.getSource();
                        final int start = s.getSpanStart(span);
                        final int end = s.getSpanEnd(span);
                        spanEnd = end;
                        
                        Log.i(TAG,"setSpanClickable , image_src = "+image_src+" , start = "+start+" , end = "+end);

                        ClickableSpan click_span = new ClickableSpan() {
                                @Override
                                public void onClick(View widget) {
                                        eText.setCursorVisible(false);
                                        Log.i(TAG , "click_span , onClick , "+eText.getSelectionStart());
                                        Toast.makeText(MainActivity.this,
                                                        "Image Clicked " + image_src, Toast.LENGTH_SHORT)
                                                        .show();
                                }
                        };

                        ClickableSpan[] click_spans = s.getSpans(start, end,
                                        ClickableSpan.class);
                        Log.i(TAG,"click_spans.length = "+click_spans.length);
                        if (click_spans.length != 0) {
                                // remove all click spans
                                for (ClickableSpan c_span : click_spans) {
                                        ((Spannable) s).removeSpan(c_span);
                                }
                        }

                        ((Spannable) s).setSpan(click_span, start, end,
                                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        Log.i(TAG,"length = "+s.getSpans(start, end,ClickableSpan.class).length);
                }
        }
        
}

效果:

技术分享

代码:这里

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。