单击ImageSpan中特定字符的事件

时间:2015-04-06 13:43:21

标签: android clickable spannablestring imagespan android-chips

我使用ImageSpan使用SpannableStringBuilder将textview显示为位图。

我已成功实施并获得以下输出。

enter image description here

现在我想要textview的特定字符的click事件,即来自textview的Cross Sign。

为此我提到了this demo。

但这会返回整个textview的click事件。我想要文本视图末尾唯一的Cross Sign的click事件。

到目前为止我尝试了什么::

final TextView tv = createMultiSelectTextView(selectValue[i]);
BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());

spannableStringBuilder.append(selectValue[i]+" ");
spannableStringBuilder.setSpan(new ImageSpan(bd), spannableStringBuilder.length()-(selectValue[i].length()+1), spannableStringBuilder.length()-1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

ClickableSpan clickSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {    
        }};
        spannableStringBuilder.setSpan(clickSpan,spannableStringBuilder.length()-(selectValue[i].length()+1), spannableStringBuilder.length()-1,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }


if (spannableStringBuilder.length() > 0) {
    button.setText(spannableStringBuilder);

    button.setMovementMethod(LinkMovementMethodExt
                    .getInstance(handler, ClickableSpan.class));

} else {
    button.setText(context.getString(R.string.msg_select)
                    + select.getFieldName());

    button.setMovementMethod(LinkMovementMethodExt
                    .getInstance(handler, ClickableSpan.class));
}

LinkMovementMethodExt类

import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;

public class LinkMovementMethodExt extends LinkMovementMethod {
    private static LinkMovementMethod sInstance;
    private  Handler handler = null;
    private  Class spanClass = null;

    public static  MovementMethod getInstance(Handler _handler,Class _spanClass) {
        if (sInstance == null) {
            sInstance = new LinkMovementMethodExt();
            ((LinkMovementMethodExt)sInstance).handler = _handler;
            ((LinkMovementMethodExt)sInstance).spanClass = _spanClass;
        }

        return sInstance;
    }
     @Override
        public boolean onTouchEvent(TextView widget, Spannable buffer,
                                    MotionEvent event) {
            int action = event.getAction();

            if (action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();

                x += widget.getScrollX();
                y += widget.getScrollY();

                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);
                /**
                 * get you interest span
                 */
                Object [] spans = buffer.getSpans(off, off, spanClass);
                if (spans.length != 0) {
                    if (action == MotionEvent.ACTION_DOWN) {
                        Selection.setSelection(buffer,
                                               buffer.getSpanStart(spans[0]),
                                               buffer.getSpanEnd(spans[0]));
                        MessageSpan obj = new MessageSpan();
                                    obj.setObj(spans);
                                    obj.setView(widget);
                        Message message = handler.obtainMessage();
                                message.obj = obj;
                                message.what = 100;
                                message.sendToTarget();
                        return true;
                    } else if (action == MotionEvent.ACTION_UP) {
                        MessageSpan obj = new MessageSpan();
                        obj.setView(widget);
                        Message message = handler.obtainMessage();
                                message.obj = obj;
                                message.what = 200;
                                message.sendToTarget();
                        return true;                                
                    }
                } 
            }

            return super.onTouchEvent(widget, buffer, event);
        }

     public boolean canSelectArbitrarily() {
            return true;
        }

    public boolean onKeyUp(TextView widget, Spannable buffer, int keyCode,
            KeyEvent event) {
        return false;
    }
}

处理Handler ::

中的响应
private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            int what = msg.what;
            BackgroundColorSpan color = null;
            if (what == 100) {
                MessageSpan ms = (MessageSpan) msg.obj;
                Object[] spans = (Object[])ms.getObj();
                TextView view = ms.getView();

                for (Object span : spans) {
                    if (span instanceof ClickableSpan) {
                        int start = Selection.getSelectionStart(view.getText());
                        int end = Selection.getSelectionEnd(view.getText());
                        Spannable _span = (Spannable)view.getText();
                        System.out.println("Cross Clicked 123456!!!");
                        color = new BackgroundColorSpan(view.getLinkTextColors().getDefaultColor());
                        _span.setSpan(color, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        view.setText(_span);
                    }
                }
            }else if (what == 200) {
                MessageSpan ms = (MessageSpan) msg.obj;
                TextView view = ms.getView();
                Spannable _span = (Spannable)view.getText();
                _span.removeSpan(color);
                view.setText(_span);
            }
        };
    };

0 个答案:

没有答案