机器人 - 如何设置部分的文字是可以点击 [英] android - How to set the portion of the text is clickable

查看:96
本文介绍了机器人 - 如何设置部分的文字是可以点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextView的。根据这种观点我希望把它作为文本的某些部分是可以点击的。如果你点击文本,然后我想打开web视图。我试过,但我没有得到。请任何人可以帮助我。

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 tried but i am not getting. please can anybody help me.

我做了以下的方法

textView.setText(Html.fromHtml(我已阅读并同意+                      条款和条件)); textView.setClickable(真正的); textView.setMovementMethod(LinkMovementMethod.getInstance());

textView.setText(Html.fromHtml("I have read and agree to the " + "TERMS AND CONDITIONS")); textView.setClickable(true); textView.setMovementMethod(LinkMovementMethod.getInstance());

在这里,如果你点击条款和条件,那么它​​在浏览器中打开,但我想在web视图中打开它。

Here if you click on the "TERMS AND CONDITIONS" then it opens in the browser but i want to open it in the webview.

感谢

推荐答案

另一种方式,借用了一下从Linkify但允许您自定义操作。

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

自定义跨度类:

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

辅助功能:

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

用法:

clickify(textView, clickText,new ClickSpan.OnClickListener()
     {
        @Override
        public void onClick() {
            // do something
        }
    });

这篇关于机器人 - 如何设置部分的文字是可以点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆