Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextView 当设置了setMovementMethod方法后 在设置Ellipsize=end属性时 省略号不展示 #725

Open
Apollo2011 opened this issue Nov 2, 2018 · 1 comment

Comments

@Apollo2011
Copy link

TextView 当设置了setMovementMethod方法后 在设置Ellipsize=end属性时 省略号不展示

@demoNo
Copy link

demoNo commented Nov 5, 2018

public class TextViewTouchListener implements View.OnTouchListener {
    Spannable spannable;

    public TextViewTouchListener(Spannable spannable) {
        this.spannable = spannable;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (!(v instanceof TextView)) {
            return false;
        }
        TextView textView = (TextView) v;
        if (action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

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

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

            Layout layout = textView.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(textView);
                } else if (action == MotionEvent.ACTION_DOWN) {
                    Selection.setSelection(spannable,
                            spannable.getSpanStart(link[0]),
                            spannable.getSpanEnd(link[0]));
                }

                return true;
            } else {
                Selection.removeSelection(spannable);
            }
        }

        return false;
    }
}

然后设置TextView,去掉setMovementMethod

textView.setText(str);
textView.setOnTouchListener(new TextViewTouchListener(str));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants