我在android textview中有一个链接。我无法捕获链接点击事件。
String text = "http:://www.google.com is a google link";
textview.setText(text);
" HTTP :: //www.google.com"这个字符串的范围可以在textview中单击。我想抓住那个特定的点击事件。
我尝试了以下内容。
public static void setTextView(TextView text, CharSequence sequence) {
UoloLogger.i(TAG, "Setting string :: "+sequence);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}
public static void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
UoloLogger.i(TAG, span.getURL());
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
我开始使用setTextView()方法将文本设置到我的textview中。我得到的URLSpan数组是空的,即使我有链接。
String text = "http:://www.google.com is a google link";
setTextView(textView, text);
抱歉英文不好。我想,我已经解释了我的问题。有人能帮我吗。
答案 0 :(得分:2)
public static void setLinkclickEvent(TextView tv, HandleLinkClickInsideTextView clickInterface) {
String text = tv.getText().toString();
String str = "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])";
Pattern pattern = Pattern.compile(str);
Matcher matcher = pattern.matcher(tv.getText());
while (matcher.find()) {
int x = matcher.start();
int y = matcher.end();
final android.text.SpannableString f = new android.text.SpannableString(
tv.getText());
InternalURLSpan span = new InternalURLSpan();
span.setText(text.substring(x, y));
span.setClickInterface(clickInterface);
f.setSpan(span, x, y,
android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(f);
}
tv.setLinksClickable(true);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setFocusable(false);
}
public static class InternalURLSpan extends android.text.style.ClickableSpan {
private String text;
private HandleLinkClickInsideTextView clickInterface;
@Override
public void onClick(View widget) {
getClickInterface().onLinkClicked(getText());
}
public void setText(String textString) {
this.text = textString;
}
public String getText() {
return this.text;
}
public void setClickInterface(HandleLinkClickInsideTextView clickInterface) {
this.clickInterface = clickInterface;
}
public HandleLinkClickInsideTextView getClickInterface() {
return this.clickInterface;
}
}
public interface HandleLinkClickInsideTextView {
public void onLinkClicked(String url);
}
在此之后我才使用方法发送点击事件。
textview.setText("http://google.com is google website and http://youtube.com is youtube site");
setLinkclickEvent(textview, new HandleLinkClickInsideTextView() {
public void onLinkClicked(String url) {
// Here I added my code
}
});
答案 1 :(得分:0)
如果要在textview点击后打开链接,有两个选项:
使用java代码:
Spanned text = Html.fromHtml("<u>GOOGLE.COM</u>");
textView.setText(text);
Uri uri = Uri.parse("http://shopwhere.com.au/");
Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
// Create and start the chooser
Intent chooser = Intent.createChooser(webIntent, "Open with");
startActivityForResult(chooser,0);
使用XML:
在textview标记内使用android:autoLink="web"
。您还可以更改链接颜色android:textColorHighlight="@android:color/transparent"
和android:textColorLink="@color/white"
。
答案 2 :(得分:0)
您可以使用SpannableStringBuilder实现相同目的。
只需初始化要添加2个或更多侦听器的TextView,然后将其传递给我创建的以下方法:
示例代码:
private void customTextView(TextView view) {
SpannableStringBuilder spanTxt = new SpannableStringBuilder(
"I agree to the ");
spanTxt.append("Term of services");
spanTxt.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(getApplicationContext(), "Terms of services Clicked",
Toast.LENGTH_SHORT).show();
}
}, spanTxt.length() - "Term of services".length(), spanTxt.length(), 0);
spanTxt.append(" and");
spanTxt.setSpan(new ForegroundColorSpan(Color.BLACK), 32, spanTxt.length(), 0);
spanTxt.append(" Privacy Policy");
spanTxt.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(getApplicationContext(), "Privacy Policy Clicked",
Toast.LENGTH_SHORT).show();
}
}, spanTxt.length() - " Privacy Policy".length(), spanTxt.length(), 0);
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(spanTxt, BufferType.SPANNABLE);
}
在您的XML中,使用android:textColorLink添加您选择的自定义链接颜色。像这样:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColorLink="#C36241" />