[Android] response to click events (SpannableString) in textview)

Source: Internet
Author: User

Project requirements

Click a short text section in textView to display a dialog box.


Solution to failure

Two textView horizontal la s are used at the beginning. It can be imagined that when the first textView occupies a full row and no rows have been changed, the second textView may have a line feed layout problem.


SpannableString

The short text is underlined.

When you click a short text in textView, the system will process the text as a url and add a blue background to the text in the clicked part.


Solution

public class TouchableSpan extends ClickableSpan {    private boolean mIsPressed;    private int mPressedBackgroundColor;    private int mNormalTextColor;    private int mPressedTextColor;    public TouchableSpan(int normalTextColor, int pressedTextColor, int pressedBackgroundColor) {        mNormalTextColor = normalTextColor;        mPressedTextColor = pressedTextColor;        mPressedBackgroundColor = pressedBackgroundColor;    }    public void setPressed(boolean isSelected) {        mIsPressed = isSelected;    }    @Override    public void updateDrawState(TextPaint ds) {        super.updateDrawState(ds);        ds.setColor(mIsPressed ? mPressedTextColor : mNormalTextColor);        ds.bgColor = mIsPressed ? mPressedBackgroundColor : 0x00eeeeee;        ds.setUnderlineText(false);    }@Overridepublic void onClick(View widget) {//todo}}
class LinkTouchMovementMethod extends LinkMovementMethod {    private TouchableSpan mPressedSpan;    @Override    public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            mPressedSpan = getPressedSpan(textView, spannable, event);            if (mPressedSpan != null) {                mPressedSpan.setPressed(true);                Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan),                        spannable.getSpanEnd(mPressedSpan));            }        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {            TouchableSpan touchedSpan = getPressedSpan(textView, spannable, event);            if (mPressedSpan != null && touchedSpan != mPressedSpan) {                mPressedSpan.setPressed(false);                mPressedSpan = null;                Selection.removeSelection(spannable);            }        } else {            if (mPressedSpan != null) {                mPressedSpan.setPressed(false);                super.onTouchEvent(textView, spannable, event);            }            mPressedSpan = null;            Selection.removeSelection(spannable);        }        return true;    }    TouchableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) {        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);        TouchableSpan[] link = spannable.getSpans(off, off, TouchableSpan.class);        TouchableSpan touchedSpan = null;        if (link.length > 0) {            touchedSpan = link[0];        }        return touchedSpan;    }}

Don't forget to add

mStartPageTermsCondition.setMovementMethod(new LinkTouchMovementMethod());



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.