目前,我有一个像
这样的字符串http://www.example.com/defg-/\nletters
我将此字符串放入 TextView,并通过 setAutoLinkMask(Linkify.WEB_URLS) 和 setMovementMethod(LinkMovementMethod.getInstance())
但是,链接被错误识别,只有
http://www.example.com/defg <--missing "-/"
高亮但不高亮
http://www.example.com/defg-/ <--I want this
,并导致错误的 url。
我应该怎么做才能正确识别 url?
示例结果(第二个链接被错误识别)
代码实现
txtNorm = (TextView) findViewById(R.id.txtNorm);
txtNorm.setText("http://www.example.com/defg-/");
txtNorm.setAutoLinkMask(Linkify.WEB_URLS);
txtNorm.setMovementMethod(LinkMovementMethod.getInstance());
txtCustom = (TextView) findViewById(R.id.txtCustom);
txtCustom.setText("http://www.example.com/defg-/\nletters");
txtCustom.setAutoLinkMask(Linkify.WEB_URLS);
txtCustom.setMovementMethod(LinkMovementMethod.getInstance());
最佳答案
我找到了一种你可以试试这个的方法.. 首先你需要知道如果你在 url 的末尾添加 -/
这不是标准 Web Url 的通用格式。所以我做了一个自定义模式..
String urlRegex="[://.a-zA-Z_-]+-/"; // carefully set your pattern.
Pattern pattern = Pattern.compile(urlRegex);
String url1="press http://www.example.com/defg-/\\ or on Android& to search it on google";
text.setText(url1);
Matcher matcher1=Pattern.compile(urlRegex).matcher(url1);
while (matcher1.find()) {
final String tag = matcher1.group(0);
Linkify.addLinks(text, pattern, tag);
}
text.setMovementMethod(LinkMovementMethod.getInstance());
关于java - Android: URL 以 -/\n 结尾的 TextView 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34563177/