gpt4 book ai didi

android - 如何同时在 textView 和自定义 URLSpan 中使用 autoLink?

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:25 30 4
gpt4 key购买 nike

我有一个这样的 textView:

<TextView
android:id="@+id/note_viewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="email|web|phone"
android:textSize="15sp" />

还有一个像这样的 Spannable 字符串:

String input = note.getText();
SpannableStringBuilder builder = new SpannableStringBuilder(input);
Pattern pattern = Pattern.compile(XTAG_PATTERN);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
String text = input.subSequence(start, end).toString();
ClickableURLSpan url = new ClickableURLSpan(text, getActivity()
.getApplicationContext(), this);
builder.setSpan(url, start, end, 0);
}
noteView.setText(builder);
noteView.setMovementMethod(LinkMovementMethod.getInstance());

还有一个像这样的自定义 `URLSpan 类:

public class ClickableURLSpan extends URLSpan {
Context context;
NoteViewFragment noteViewFragment;
public ClickableURLSpan(String url,Context c, NoteViewFragment noteViewFragment) {
super(url);
context=c;
this.noteViewFragment=noteViewFragment;
}
@Override
public void onClick(View widget) {
String clickedText = getURL();
Intent i=new Intent(context,SearchActivity.class);
i.putExtra("tag", clickedText);
noteViewFragment.startActivity(i);
}}

但是当我使用自动链接时,我的自定义可点击范围不起作用。如何同时使用自动链接和我自己的可点击范围?

最佳答案

将可生成的字符串更改为:

            String input = note.getText();
Spannable spannable = new SpannableString(Html.fromHtml(input));
Pattern pattern = Pattern.compile(XTAG_PATTERN);

Linkify.addLinks(spannable, pattern, "");
URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
for (URLSpan urlSpan : spans) {
ClickableURLSpan linkSpan = new ClickableURLSpan(text, getActivity().getApplicationContext(), this);
int spanStart = spannable.getSpanStart(urlSpan);
int spanEnd = spannable.getSpanEnd(urlSpan);
spannable.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.removeSpan(urlSpan);
}
textview.setMovementMethod(EnhancedLinkMovementMethod.getInstance());
noteView.setText(spannable, TextView.BufferType.SPANNABLE);

关于android - 如何同时在 textView 和自定义 URLSpan 中使用 autoLink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22719803/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com