0

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

我已经成功实现它并得到以下输出。

在此处输入图像描述

现在我想要文本视图的特定字符的点击事件,即来自文本视图的十字符号。

为此,我参考了这个演示。

但这会返回整个文本视图的单击事件。我想要文本视图末尾的唯一十字符号的单击事件。

到目前为止我所尝试的::

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);
            }
        };
    };
4

0 回答 0