我有一个文本视图,里面有一个链接。setMovementMethod
在代码中,当用户单击文本时,我调用打开链接。但它会在默认浏览器或浏览器选择器中打开它。
如何使用带有可点击文本视图的 chrome 自定义标签?
我有一个文本视图,里面有一个链接。setMovementMethod
在代码中,当用户单击文本时,我调用打开链接。但它会在默认浏览器或浏览器选择器中打开它。
如何使用带有可点击文本视图的 chrome 自定义标签?
这是因为为每个链接文本模式TextView
创建了。一旦找到 url ,它就会调用. 此事件启动意图,这就是您看到默认浏览器启动的原因。URLSpan
ClickableSpan
MovementMethod
onClick
URLSpan
ACTION_VIEW
您可以做的是编写您自己的实现,URLSpan
您将在其中覆盖 onClick 方法并CustomTabs
从那里启动服务。
首先创建URLSpan
将覆盖onClick
方法的自定义:
public class CustomTabsURLSpan extends URLSpan {
public CustomTabsURLSpan(String url) {
super(url);
}
public CustomTabsURLSpan(Parcel src) {
super(src);
}
@Override
public void onClick(View widget) {
String url = getUrl();
//attempt to open in CustomTabs, if that fails call super.onClick(widget);
}
}
创建自定义转换方法,将跨度设置为链接:
public class LinkTransformationMethod implements TransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
if (view instanceof TextView) {
TextView textView = (TextView) view;
Linkify.addLinks(textView, Linkify.WEB_URLS);
String stringText = textView.getText().toString();
Spannable text = (Spannable) textView.getText();
URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
for (int i = spans.length - 1; i >= 0; i--) {
URLSpan oldSpan = spans[i];
text.removeSpan(oldSpan);
String url = oldSpan.getURL();
int startIndex = stringText.indexOf(url);
int lastIndex = startIndex + url.length();
text.setSpan(new CustomTabsURLSpan(url), startIndex, lastIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return text;
}
return source;
}
@Override
public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {
}
}
这里是快速解释:https ://medium.com/@nullthemall/make-textview-open-links-in-customtabs-12fdcf4bb684#.ig1chpbbe
我已经稍微改变了 Nikola 的答案,以便它仍然适用于其他 linkify 选项。
我添加Patterns.WEB_URL.matcher()
了一个构造函数来设置你想要的链接选项。
用法:
textView.setTransformationMethod(new LinkTransformationMethod(Linkify.WEB_URLS |
Linkify.EMAIL_ADDRESSES |
Linkify.PHONE_NUMBERS));
完整的类本身:
public class LinkTransformationMethod implements TransformationMethod {
private final int linkifyOptions;
public LinkTransformationMethod(int linkifyOptions) {
this.linkifyOptions = linkifyOptions;
}
@Override
public CharSequence getTransformation(CharSequence source, View view) {
if (view instanceof TextView) {
TextView textView = (TextView) view;
Linkify.addLinks(textView, linkifyOptions);
if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
return source;
}
Spannable text = (Spannable) textView.getText();
URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
for (int i = spans.length - 1; i >= 0; i--) {
URLSpan oldSpan = spans[i];
int start = text.getSpanStart(oldSpan);
int end = text.getSpanEnd(oldSpan);
String url = oldSpan.getURL();
if (!Patterns.WEB_URL.matcher(url).matches()) {
continue;
}
text.removeSpan(oldSpan);
text.setSpan(new ChromeTabsUrlSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return text;
}
return source;
}
@Override
public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {
}
}