12

我有一个如下的文本视图:

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

这只是一个大文本。所以,我使用 marquee 水平滚动文本。效果很好。我的问题是,如何在单击选定的滚动文本时调用单击事件。

说前任:

  1. 当用户在上面的文本视图中单击“注册”一词时,我必须调用新的 Intent。
  2. 当用户点击这个词"Terms"时,我必须调用另一个新的 Intent(一个带有 webview as Terms 的活动URL Link)。

由于“注册”和“条款”是 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);

上面的代码工作正常,当我点击“条款”这个词时,它把我带到了浏览器,但我想去新的活动。

4

5 回答 5

16

最后,

我找到了解决方案,

这是解决方案:

    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。

于 2014-02-25T07:05:06.067 回答
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});
于 2018-07-03T09:58:16.850 回答
0

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

Step1-:您的文本将在 SpannableString

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

Step2:-像这样在ClickableSpan中添加点击

 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 的单击,其中包含字符开始和结束索引,例如 Registering word in start at 3rd position and end at 11 所以添加单击以注册单词

 ss.setSpan(Registering , 3, 11, 0);

在此之后的术语相同,在您的 TextView 上添加您的 SpannableString

  textview.setMovementMethod(LinkMovementMethod.getInstance());
    textview.setText(ss, TextView.BufferType.SPANNABLE);
    textview.setSelected(true);
于 2018-12-04T20:05:33.573 回答
0

我建议下面的可点击字符串代码TextView是动态的。此代码的优点是,如果您String多次单击相同的代码,则可以单击两者Strings。例如,如果您想设置 click 并且 StringBoy正在打板球。Boy正在踢足球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);

    }
于 2019-05-17T06:27:59.023 回答
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);
            }
        };
于 2021-01-15T17:12:53.810 回答