当autoLink设置为“web”时,TextView会点击链接。 [英] TextView catch clicking on link when autoLink is set to "web"
问题描述
我的布局中有TextView,其中 autoLink ='web'
当我点击它时,内部互联网浏览器会打开,其中包含以文本形式提供的URL TextView,一切都很好。我希望用它做更多的事情,即除了在浏览器中打开那个URL我想做一些额外的动作。最痛苦的方法是什么?
I have TextView in my layout with autoLink='web'
and when I click on it, internal internet browser is opened with URL that is provided as a text in that TextView and everything is fine. What I wish is to do something more with it, i.e. beside opening that URL in browser I want to do some additional actions. What is the least painful way to do it?
最好,
Deveti
推荐答案
实际上,
我通过放弃 autolLink ='web解决了这个问题'
并解析html url,创建 SpannableString
并设置其他跨度。
I solved this by abandoning autolLink='web'
and by parsing html url, creating SpannableString
and setting additional spans.
所以,假设我想要执行以下操作:
So, let's say that I want to do the following:
- 如果用户点击textView文本中不是url的部分,方法
someMethod()
应该被调用 - 如果用户点击textView文本的部分是url,方法
someMethod()
应该被调用,浏览器应该用给定的URL打开
- if user clicks on the part of the text of textView which is not url, method
someMethod()
should be called - if user clicks on the part of the text of textView which is url, method
someMethod()
should be called and browser should be openned with given URL
所以,首先我们拥有变量消息
中的内容和方法 String extractFirstLink(String text)
将用于获取网址。
So, first we have the content which is in the variable message
and method String extractFirstLink(String text)
will be used to get the url.
private static String extractFirstLink(String text) {
Matcher m = Patterns.WEB_URL.matcher(text);
return m.find() ? m.group() : null;
}
注意:就我而言,我知道我只有一个网址,但是如果你可以期待多个网址,这个解决方案(在StackOverflow上找不到,现在找不到它)将完成工作:
Note: in my case I know that I will have only one url, but if you can expect more than one url, this solution (found somewhere on StackOverflow, can't find it right now) will do the work:
public static String[] extractLinks(String text) {
List<String> links = new ArrayList<String>();
Matcher m = Patterns.WEB_URL.matcher(text);
while (m.find()) {
String url = m.group();
links.add(url);
}
return links.toArray(new String[links.size()]);
}
所以,这是代码:
int contentTextColor = ...; // color of non-url text in textView
int urlTextColor = ...;
String message = notification.getMessage(); // content stored
final String foundLink = extractFirstLink(message);
SpannableString styledString = new SpannableString(message);
if (foundLink != null) {
int startPosition = message.indexOf(foundLink);
int endPosition = startPosition + foundLink.length();
styledString.setSpan(new ForegroundColorSpan(urlTextColor), startPosition, endPosition, 0);
ClickableSpan clickableURLSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
someMethod();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(foundLink));
widget.getContext().startActivity(browserIntent);
}
};
ClickableSpan clickableNonURLSpan1 = new ClickableSpan() {
@Override
public void onClick(View view) {
someMethod();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
};
ClickableSpan clickableNonURLSpan2 = new ClickableSpan() {
@Override
public void onClick(View view) {
someMethod();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
};
styledString.setSpan(clickableURLSpan, startPosition, endPosition, 0);
if (startPosition > 0) {
styledString.setSpan(clickableNonURLSpan1, 0, startPosition - 1, 0);
styledString.setSpan(new ForegroundColorSpan(contentTextColor), 0, startPosition -1, 0);
}
if (endPosition < message.length() -1) {
styledString.setSpan(clickableNonURLSpan2, endPosition + 1, message.length(), 0);
styledString.setSpan(new ForegroundColorSpan(contentTextColor), endPosition + 1, message.length(), 0);
}
txtView.setMovementMethod(LinkMovementMethod.getInstance());
}
txtView.setText(styledString);
这里有一些有趣的事情。例如,当我希望跨度响应点击时,我必须使用 ClickableSpan
,但是!我必须处理格式为text1-url-text2的情况。所以,由于所有这三个部分都应该是 ClickableSpan
s而text1和text2不应该有下划线,这是它的默认行为,我不得不重写 updateDrawState
我在text1和text2的 ClickableSpan
实现中的方法。
There are some interesting things here. For example, when I wish to make span responsive to click, I have to use ClickableSpan
, but! I must handle situation where I have format text1-url-text2. So, since all these three parts should be ClickableSpan
s and text1 and text2 should not have underline, which is the default behaviour of it, I had to override updateDrawState
method in my implementation of ClickableSpan
for text1 and text2.
还有另一个问题,我必须创建两个ClickableSpan接口实现才能按预期工作。这就是我创建 clickableNonURLSpan1
和 clickableNonURLSpan2
的原因。
There is another catch, I had to create two implementation of ClickableSpan interface in order to have to work as intended. That's why I created clickableNonURLSpan1
and clickableNonURLSpan2
.
这篇关于当autoLink设置为“web”时,TextView会点击链接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!