Android 在单个文本视图中的多个链接上设置onclick
我在数据库中有一个字符串,如下所示:Android 在单个文本视图中的多个链接上设置onclick,android,hyperlink,xamarin,textview,clickable,Android,Hyperlink,Xamarin,Textview,Clickable,我在数据库中有一个字符串,如下所示: string f = "this is the <a href="/page1"> first link</a> and this is the <a href="/page1"> second link</a>" textview1.TextFormatted = Html.FromHtml(f); url =? Intent i = new Intent(Android.Conten
string f = "this is the <a href="/page1"> first link</a> and this is the <a href="/page1"> second link</a>"
textview1.TextFormatted = Html.FromHtml(f);
url =?
Intent i = new Intent(Android.Content.Intent.ActionView,url);
StartActivity(i);
string f=“这是,这是”
textview1.TextFormatted=Html.FromHtml(f);
url=?
Intent i=新的Intent(Android.Content.Intent.ActionView,url);
星触觉(i);
字符串中的链接数不同。我想使textview中的所有链接都可单击,并且当用户单击每个链接时,该链接的url将发送到另一个活动。当您使用Html.fromHtml设置文本时,textview中的“”将替换为url跨距。 您可以获取每个url跨距,并为onClick函数设置可单击跨距
有关解决方案代码,请参阅。我使用SpannableStringBuilder实现了相同的功能 只需初始化要添加2个或更多侦听器的TextView,然后将其传递给我创建的以下方法:
private void customTextView(TextView view) {
SpannableStringBuilder spanTxt = new SpannableStringBuilder(
getString(R.string.read_and_accept));
spanTxt.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.black_30)), 0,
getString(R.string.read_and_accept).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanTxt.append(" ");
spanTxt.append(getString(R.string.t_and_c));
spanTxt.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Utils.redirectUserToUrl(activity,"http://luxit-driver-terms.tookan.in");
}
}, spanTxt.length() - getString(R.string.t_and_c).length(), spanTxt.length(), 0);
spanTxt.append(" and ");
spanTxt.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.accent)), 48, spanTxt.length(), 0);
spanTxt.append(getString(R.string.privacy_policy));
spanTxt.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Utils.redirectUserToUrl(activity,"http://luxit-driver-privacypolicy.tookan.in/");
}
}, spanTxt.length() - getString(R.string.privacy_policy).length(), spanTxt.length(), 0);
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(spanTxt, TextView.BufferType.SPANNABLE);
}
在Xml中,使用android:textColorLink为ur链接文本添加自定义颜色
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textColorLink="#000000" />