我在android textview中有一個鏈接。我無法捕獲鏈接點擊事件。在android textview中捕獲http鏈接點擊事件
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);
對不起,這個英文不好。我想,我已經解釋了我的問題。有人能幫我嗎。
遵照Arunkumar https://stackoverflow.com/questions/7722806/get-the-value-of-link-text-when-clicked-in-a-textview-in-android這裏給出的步驟 –
Follow this: https://stackoverflow.com/questions/7722806/get-the-value-of-link-text-when-clicked-in-a-textview-in-android –