android – 如何在editText中处理imageSpan上的onClick事件?

前端之家收集整理的这篇文章主要介绍了android – 如何在editText中处理imageSpan上的onClick事件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个应用程序,用户从库中选择一个图像,它将添加到editText中,现在我想如果用户点击editText中的图像,它应该在fullScreen中打开,我使用下面的代码: –
public void addToEdt(Bitmap bitmap){
    SpannableString ss = new SpannableString("abc");
    Drawable d = new BitmapDrawable(getResources(),bitmap);
    d.setBounds(0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
    ImageSpan span = new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
    ss.setSpan(span,3,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    edt_note.setTransformationMethod(null);
    edt_note.getText().insert(edt_note.getSelectionStart(),ss);

    final int start = ss.getSpanStart(span);
    final int end = ss.getSpanEnd(span);

    ClickableSpan click_span = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
                   Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
        }
    };

    ClickableSpan[] click_spans = ss.getSpans(start,end,ClickableSpan.class);

    if(click_spans.length != 0) {
        // remove all click spans
        for (ClickableSpan c_span : click_spans) {
            ss.removeSpan(c_span);
        }
    }

    ss.setSpan(click_span,start,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

尝试上面的代码,但它没有监听onClick事件,
现在,我如何在此图像上收听点击事件并执行进一步的任务?

解决方法

可编辑的跨度位于editText的相同开始和结束位置.
sb.setSpan(cs,imageStartSpan,imageEndSpan,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

editText.setMovementMethod(LinkMovementMethod.getInstance());

我无法为你编写完整的代码.试试以下样本: –

public void addToEdt(Bitmap bitmap){
    SpannableString ss = new SpannableString();
    Drawable d = new BitmapDrawable(getResources(),d.getIntrinsicHeight());
    ss.append("abc"); // Append the text here
    ImageSpan span = new ImageSpan(d,2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // start(0) and end (2) will create an image span over abc text
    ss.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    ss.delete(0,2);
                    editText.setText(ss);
                }
            },Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // this will add a clickable span and on click will delete the span and text
    editText.setText(ss); // this will show your image/clickable span in edittext
}


editText.setMovementMethod(LinkMovementMethod.getInstance());
原文链接:https://www.f2er.com/android/316457.html

猜你在找的Android相关文章