android - How to set the portion of the text is cl

2019-07-25 11:36发布

I have one TextView. In this view I want to make it as some portion of text is clickable. if you click on that text then I want to open WebView.

I did the following way:

textView.setText(Html.fromHtml("I have read and agree to the " +
                     "<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Here if you click on the TERMS AND CONDITIONS then it opens in the browser but I want to open it in the WebView.

4条回答
女痞
2楼-- · 2019-07-25 12:28

Another way, borrows a bit from Linkify but allows you to customize your handling.

Custom Span Class:

public class ClickSpan extends ClickableSpan {

    private OnClickListener mListener;

    public ClickSpan(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
       if (mListener != null) mListener.onClick();
    }

    public interface OnClickListener {
        void onClick();
    }
}

Helper function:

public static void clickify(TextView view, final String clickableText, 
    final ClickSpan.OnClickListener listener) {

    CharSequence text = view.getText();
    String string = text.toString();
    ClickSpan span = new ClickSpan(listener);

    int start = string.indexOf(clickableText);
    int end = start + clickableText.length();
    if (start == -1) return;

    if (text instanceof Spannable) {
        ((Spannable)text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        SpannableString s = SpannableString.valueOf(text);
        s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(s);
    }

    MovementMethod m = view.getMovementMethod();
    if ((m == null) || !(m instanceof LinkMovementMethod)) {
        view.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

Usage:

clickify(textView, clickText,new ClickSpan.OnClickListener()
     {
        @Override
        public void onClick() {
            // do something
        }
    });
查看更多
神经病院院长
3楼-- · 2019-07-25 12:29

How do I make links in a TextView clickable?

or u can create a linear layout with horizontal orientation having 2 textviews making second textview clickable..

查看更多
姐就是有狂的资本
4楼-- · 2019-07-25 12:33

Why don't you make the textView call on onClick method:

<TextView
    ...
    android:onClick"openWebView"
    ...
/>

And then just have a method in your activity called:

public void openWebView (View v) {
    ....
    // Do something
}
查看更多
手持菜刀,她持情操
5楼-- · 2019-07-25 12:40

try this may it works

   SpannableString span = new SpannableString(
        "Click here to for gmail page.");


    span.setSpan(new URLSpan("http://www.gmail.com"), 6, 10,
               Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView tv = (TextView) findViewById(R.id.text);
    tv.setText(span);
    tv.setMovementMethod(LinkMovementMethod.getInstance());

change start and end position according to your text size

查看更多
登录 后发表回答