如何点击或点击不同的字一个TextView文字? [英] How to click or tap on a TextView text on different words?

查看:107
本文介绍了如何点击或点击不同的字一个TextView文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何移动到另一个视图通过点击文本视图以两个不同的词。 这是我使用的字符串。

How to move to another view by click on text view with two different words. this is the string i am using.

By clicking Sign Up, you are indicating that you have read and agree to the 
Term of Use and Privacy Policy.

我想使这两个词(使用期限,隐私政策)的不同颜色和点击。

i want to make these two words (Term of Use, Privacy Policy) in different color and clickable..

我知道浩使颜色一个特定的词。我想使它点击。

i know ho to make color for a particular word. i want to make it clickable .

推荐答案

我终于想通了如何在一个TextView多个可点击的部分。 重要的是,他们都有自己的ClickableSpan!这就是我想歪了第一次测试。如果他们有相同的ClickableSpan情况下,只有最后一组跨度记住了。

I finally figured it out how to have multiple clickable parts in a TextView. It is important that they all have their own ClickableSpan! That is where I went wrong the first time testing it. If they have the same ClickableSpan instance, only the last set span is remembered.

我创建了所需的点击区域的由[和]。

I created a String with the required clickable area's surrounded by "[" and "]".

String sentence = "this is [part 1] and [here another] and [another one]";

和这里是集TextView的,该setMovementMehthod也是必需的:

and here is the set TextView, the setMovementMehthod is also mandatory:

textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(addClickablePart(sentence), BufferType.SPANNABLE);

我创造了这个功能,这将处理创建可点击区域的的:

I have created this function, which will handle the creation of the clickable area's:

private SpannableStringBuilder addClickablePart(String str) {
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);

    int idx1 = str.indexOf("[");
    int idx2 = 0;
    while (idx1 != -1) {
        idx2 = str.indexOf("]", idx1) + 1;

        final String clickString = str.substring(idx1, idx2);
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                Toast.makeText(getView().getContext(), clickString,
                        Toast.LENGTH_SHORT).show();
            }
        }, idx1, idx2, 0);
        idx1 = str.indexOf("[", idx2);
    }

    return ssb;
}

这篇关于如何点击或点击不同的字一个TextView文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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