如何为单个TextView设置多个点击事件?

13

我有一个文本视图,就像下面这样:

txtByRegistering.setText("By Registering you agree to terms and condition and privacy policy");

这是一段很长的文本。因此,我使用了跑马灯来水平滚动文本。这很好用。我的问题是,如何在单击所选滚动文本时调用单击事件。

比如:

  1. 当用户点击上面文本中的单词"Registering"时,我必须调用新的Intent。
  2. 当用户点击单词"Terms"时,我必须调用另一个新的Intent(一个具有Web视图为术语的活动具有URL链接)。

由于单词“Registering”和“Terms”是Web URL,所以我尝试了以下方法:

    String mRegDesc = "By registering you agree to the " + "<a href=\""
            + Constant.URL + "/terms_and_conditions"
            + "\">Terms of Use</a> " + "and " + "<a href=\"" + Constant.URL
            + "/privacy" + "\">Privacy Policy</a> ";

    txtByRegistering.setText(Html.fromHtml(mRegDesc));
    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setSelected(true);
    txtByRegistering.setTypeface(mTyFaceOverLockReg, Typeface.BOLD);

上述代码运行正常,当我点击“条款”一词时,它将我带到浏览器,但我希望跳转到新的活动。


我认为你需要使用 ClickableSpan,请查看我的回答:https://dev59.com/T3vZa4cB1Zd3GeqP-QtU#20988922 - Shayan Pourvatan
@Shayanpourvatan 我能为单个SpannableString设置多个ClickableSpan吗?您能否详细解释一下?另外,SpannableString支持API级别8吗? - Rethinavel
是的,您可以在一个字符串上设置多个可点击项。这种方法已添加到API级别1,欲知更多信息请参见http://developer.android.com/reference/android/text/style/ClickableSpan.html和https://dev59.com/PGLVa4cB1Zd3GeqPuj8X。 - Shayan Pourvatan
5个回答

17

最后,我找到了解决方案,

以下是解决方案:

    SpannableString SpanString = new SpannableString(
            "By Registering you agree to the Terms of Use and Privacy Policy");

    ClickableSpan teremsAndCondition = new ClickableSpan() {
        @Override
        public void onClick(View textView) {


            Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
            mIntent.putExtra("isTermsAndCondition", true);
            startActivity(mIntent);

        }
    };

   // Character starting from 32 - 45 is Terms and condition. 
   // Character starting from 49 - 63 is privacy policy. 

    ClickableSpan privacy = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
            mIntent.putExtra("isPrivacyPolicy", true);
            startActivity(mIntent);

        }
    };

    SpanString.setSpan(teremsAndCondition, 32, 45, 0);
    SpanString.setSpan(privacy, 49, 63, 0);
    SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 32, 45, 0);
    SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 49, 63, 0);
    SpanString.setSpan(new UnderlineSpan(), 32, 45, 0);
    SpanString.setSpan(new UnderlineSpan(), 49, 63, 0);

    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setText(SpanString, BufferType.SPANNABLE);
    txtByRegistering.setSelected(true);
感谢Shayan pourvatan。

有什么想法可以改变下划线颜色吗? - Sagar Chavada

2
假设这里是您的完整字符串
通过注册,我同意使用条款和隐私政策。要使以下内容可点击:
使用条款和隐私政策。 这是我的方法……
ClickableSpan terms = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        new Utils(getActivity()).shortToast("Terms");

    }
};

ClickableSpan privacy = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        new Utils(getActivity()).shortToast("Privacy");

    }
};

这个的主要功能是:
public void setClickableString(String wholeValue, TextView textView, final String[] clickableValue, ClickableSpan[] clickableSpans) {
    SpannableString spannableString = new SpannableString(wholeValue);

    for (int i = 0; i < clickableValue.length; i++) {
        ClickableSpan clickableSpan = clickableSpans[i];
        String link = clickableValue[i];

        int startIndexOfLink = wholeValue.indexOf(link);
        spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setHighlightColor(
            Color.TRANSPARENT); // prevent TextView change background when highlight
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

以下是函数调用的代码:

setClickableString(getString(R.string.terms_and_policy), tv_terms, new String[]{"Terms of Conditions", "Privacy Policy"}, new ClickableSpan[]{terms, privacy});

嫁给我吧!眨眼 你是最棒的 - The Billionaire Guy

0
此外,如果您想动态地知道用户单击了哪个文本,请使用以下方法。
 ClickableSpan listener = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                TextView tv = (TextView) textView;
                Spanned s = (Spanned) tv.getText();
                int start = s.getSpanStart(this);
                int end = s.getSpanEnd(this);
                String clickedText = s.toString().substring(start,end);
            }
        };

0

使用这个,它对我有效,在单个TextView中进行两次点击

步骤1:您的文本将在SpannableString中

SpannableString ss = new SpannableString("By Registering you agree to terms and condition and privacy policy");

步骤2:像这样在ClickableSpan中添加click。
 ClickableSpan Registering = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Intent intent=new Intent(this,WebView_Activity.class);
                            startActivity(intent);
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
        }
    };
    ClickableSpan terms = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Intent intent=new Intent(this,WebView_Activity.class);
                            startActivity(intent);
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
        }
    };

最后一步是将您的点击添加到SpannableString中,使用字符起始和结束索引,例如在第3个位置开始注册单词并在第11个位置结束,因此为注册单词添加点击。
 ss.setSpan(Registering , 3, 11, 0);

在此术语后添加您的SpannableString到TextView上。
  textview.setMovementMethod(LinkMovementMethod.getInstance());
    textview.setText(ss, TextView.BufferType.SPANNABLE);
    textview.setSelected(true);

0
我建议在TextView中使用以下代码实现可点击字符串,它是动态的。 这个代码的优点是,如果您有多个相同的字符串,您可以在这些字符串上进行单击。例如,如果您想要设置点击并且字符串为“Boy is playing cricket. Boy is playing football”,那么Boy出现了两次,这两个单词都是可点击的。
    public void setClicksOnString(String completeString, List<String> stringsToClick, TextView textView) {
        SpannableString spannableString = new SpannableString(completeString);
        for (int m = 0; m < stringsToClick.size(); m++) {
            Matcher matcher = Pattern.compile(stringsToClick.get(m)).matcher(spannableString);
            while (matcher.find()) {
                ClickableSpan stringClick = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        //you compare the string and your click logics

                    }
                };
                spannableString.setSpan(stringClick, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }
        }
        textView.setHighlightColor(
                Color.TRANSPARENT); // prevent TextView change background when highlight
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString, TextView.BufferType.SPANNABLE);

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接