我的textview如下:
txtByRegistering.setText("By Registering you agree to terms and condition and privacy policy");
这只是一个大文本。所以,我用marquee水平滚动文本。工作正常。我的问题是,如何在单击选定的滚动文本时调用click事件。
说出来:
"Terms"
一词时,我必须
调用另一个新的Intent(具有webview的活动,因为条款有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);
上面的代码工作正常,当我点击“条款”这个词时它会把我带到浏览器但我希望转到新的活动。
答案 0 :(得分:11)
最后,
我找到了解决方案,
以下是解决方案:
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。
答案 1 :(得分:0)
假设这里是您的完整字符串
注册即表示我同意条款和隐私政策
您要使其可点击的字符串是
条件条款和隐私权政策
所以,这是我的把戏.....
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});
答案 2 :(得分:0)
使用这一功能对我有用,只需单击两次TextView即可
第1步:您的文本将在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上添加带有字符开头和结尾索引的单击,例如在第3个位置处开始注册单词并在11处结束,因此添加单击以注册单词
ss.setSpan(Registering , 3, 11, 0);
在此之后将相同的用语添加到TextView上
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(ss, TextView.BufferType.SPANNABLE);
textview.setSelected(true);
答案 3 :(得分:0)
我建议下面的代码TextView
中的可点击字符串是动态的。
此代码的优点是,如果多次使用相同的String
,则可以同时单击两个Strings
。例如,如果您要设置click且String为 Boy
,则在打板球。 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);
}
答案 4 :(得分: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);
}
};