美文网首页
解决 setMovementMethod 和 Ellipsize

解决 setMovementMethod 和 Ellipsize

作者: WotYang | 来源:发表于2018-12-14 10:09 被阅读0次

    方法一:
    TextView 重写以下方法

    @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (question != null) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_UP) {
                    CharSequence text = getText();
                    if (text instanceof SpannableStringBuilder || text instanceof SpannedString
                            || text instanceof SpannableString) {
                        int x = (int) event.getX();
                        int y = (int) event.getY();
    
                        x -= getTotalPaddingLeft();
                        y -= getTotalPaddingTop();
    
                        x += getScrollX();
                        y += getScrollY();
    
                        int line = getLayout().getLineForVertical(y);
                        int off = getLayout().getOffsetForHorizontal(line, x);
    
                        ClickableSpan[] spans= getSpans(text, off);
    
                        if (spans.length != 0) {
                            link[0].onClick(this);
                            return true;
                        }
                    }
                    //todo 处理TextView事件
                }
            }
            return true;
        }
    
        private ClickableSpan[] getSpans(CharSequence text, int off) {
            ClickableSpan[] spans;
            if (text instanceof SpannableStringBuilder) {
                spans= ((SpannableStringBuilder) text).getSpans(off, off, QuestionClickableSpan.class);
            } else if (text instanceof SpannableString) {
                spans= ((SpannableString) text).getSpans(off, off, QuestionClickableSpan.class);
            } else {
                spans= ((SpannedString) text).getSpans(off, off, QuestionClickableSpan.class);
            }
            return spans;
        }
    

    方法二:

    public class LinkMovementMethod extends LinkMovementMethod {
    
        private static LinkMovementMethod instance;
    
        public static LinkMovementMethod getInstance() {
            if (instance == null) {
                instance = new QuestionLinkMovementMethod();
            }
            return instance;
        }
    
        @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);
    
                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
                if (link.length != 0) {
                    if (action == MotionEvent.ACTION_UP) {
                        link[0].onClick(widget);
                    }
                    return true;
                }
            }
            return true;
        }
    }
    
    public class OnGlobalLayoutListenerByEllipsize implements ViewTreeObserver.OnGlobalLayoutListener {
    
        private TextView textView;
        private int maxLines;
    
        public OnGlobalLayoutListenerByEllipsize(TextView textView, int maxLines) {
            if (maxLines <= 0) {
                throw new IllegalArgumentException("maxLines不能小于等于0");
            }
            this.textView = textView;
            this.maxLines = maxLines;
            this.textView.setMaxLines(this.maxLines + 1);
            this.textView.setSingleLine(false);
        }
    
        @Override
        public void onGlobalLayout() {
            if (textView.getLineCount() > maxLines) {
                int line = textView.getLayout().getLineEnd(maxLines - 1);
                //定义成CharSequence类型,是为了兼容emoji表情,如果使用String类型则会造成emoji无法显示
                CharSequence truncate = "...";
                CharSequence text = textView.getText();
                try {
                    text = text.subSequence(0, line - 2);
                } catch (Exception e) {
                    truncate = "";
                    text = textView.getText();
                }
                TextUtils.TruncateAt at = textView.getEllipsize();
                if (at == TextUtils.TruncateAt.START) {
                    textView.setText(truncate);
                    textView.append(text);
                } else if (at == TextUtils.TruncateAt.MIDDLE) {
                    textView.setText(text.subSequence(0, text.length() / 2));
                    textView.append(truncate);
                    textView.append(text.subSequence(text.length() / 2, text.length()));
                } else {
                    textView.setText(text);
                    textView.append(truncate);
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:解决 setMovementMethod 和 Ellipsize

          本文链接:https://www.haomeiwen.com/subject/gmywhqtx.html