[英]Chrome custom tabs and textview
I have a text view with a link inside it. 我有一个文本视图,里面有一个链接。 In code I call the
setMovementMethod
to open the link when the user clicks on the text. 在代码中,我调用
setMovementMethod
来在用户单击文本时打开链接。 But it opens it in the default browser or the browser chooser. 但它会在默认浏览器或浏览器选择器中打开它。
How can I use chrome custom tabs with a clickable textview? 如何使用带有可点击文本视图的Chrome自定义标签?
This is because the TextView
creates URLSpan
which is ClickableSpan
for each link text pattern. 这是因为
TextView
创建URLSpan
这是ClickableSpan
每个链接的文本模式。 Once the MovementMethod
finds url it calls onClick
method of the URLSpan
. 一旦
MovementMethod
找到url,它就会调用URLSpan
onClick
方法。 This event starts the ACTION_VIEW
intent, that's why you see the default browser starting instead. 此事件启动
ACTION_VIEW
意图,这就是您看到默认浏览器启动的原因。
What you could do is write your own implementation of the URLSpan
, where you'd override onClick method and start the CustomTabs
service from there. 您可以做的是编写自己的
URLSpan
实现,您可以在其中覆盖onClick方法并从那里启动CustomTabs
服务。
First create custom URLSpan
that will override onClick
method: 首先创建将覆盖
onClick
方法的自定义URLSpan
:
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);
}
}
Create custom transformation method, that will set spans to the links: 创建自定义转换方法,将方法设置为链接:
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) {
}
}
And here is quick explanation: https://medium.com/@nullthemall/make-textview-open-links-in-customtabs-12fdcf4bb684#.ig1chpbbe 以下是快速解释: https : //medium.com/@nullthemall/make-textview-open-links-in-customtabs-12fdcf4bb684#.ig1chpbbe
I've altered Nikola's answer a bit so that it still will work for other linkify options too. 我已经改变了Nikola的答案,所以它仍然适用于其他的linkify选项。
I've added Patterns.WEB_URL.matcher()
and a constructor to set which linkify options you want. 我添加了
Patterns.WEB_URL.matcher()
和一个构造函数来设置你想要的链接选项。
Usage: 用法:
textView.setTransformationMethod(new LinkTransformationMethod(Linkify.WEB_URLS |
Linkify.EMAIL_ADDRESSES |
Linkify.PHONE_NUMBERS));
The complete class itself: 完整的课程本身:
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) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.