如何单击或点击不同单词上的 TextView 文本? [英] How to click or tap on a TextView text on different words?

查看:13
本文介绍了如何单击或点击不同单词上的 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天全站免登陆