我有一个textview,如下所示:
txtByRegistering.setText("By Registering you agree to terms and condition and privacy policy");
这只是一个大文本.所以,我用marquee水平滚动文本.工作正常.我的问题是,如何在单击选定的滚动文本时调用click事件.
说出来:
当用户在上面的textview中单击"注册"一词时,我必须调用新的Intent.
当用户单击该单词时"Terms"
,我必须调用另一个新的Intent(具有术语的webview活动URL Link
).
由于"注册"和"术语"这个词是Web URL,我尝试过如下所示:
String mRegDesc = "By registering you agree to the " + "Terms of Use " + "and " + "Privacy Policy "; txtByRegistering.setText(Html.fromHtml(mRegDesc)); txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance()); txtByRegistering.setSelected(true); txtByRegistering.setTypeface(mTyFaceOverLockReg, Typeface.BOLD);
上面的代码工作正常,当我点击"条款"这个词时它会把我带到浏览器但是我希望转到新的活动.
最后,
我找到了解决方案,
这是解决方案:
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.