有什么方法可以在 Android 中使用 WebView 自动链接 URL 和电话号码吗? [英] Is there any way to have WebView auto-link URLs and phone numbers in Android?

查看:29
本文介绍了有什么方法可以在 Android 中使用 WebView 自动链接 URL 和电话号码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果页面上有一个不是链接的 URL 或电话号码,有没有办法让 WebView 识别它并自动将其转换为链接,就像使用 TextViews?

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?

使用 TextView,您只需将 android:autoLink 设置为所需的设置:

With a TextView you would simply set the android:autoLink to the desired settings:

<TextView
    android:autoLink="web|phone"
    ... />

但我找不到 WebView 的任何等效项.

but I can't find any equivalent for WebView.

推荐答案

我不知道有什么方法可以通过更改设置来使此工作正常进行,但解决方法是等到网页完成加载并然后做:

I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:

yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");

这会将 javaScript 注入到已加载的网页中.这里有一个稍长的示例:http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.

This will inject javaScript into the already loaded web page. There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.

如果您查看用于 Greasemonkey 的 Linkify 脚本的源代码(它是 Firefox 的插件,以防您不熟悉它),则可以找到用于创建链接的 JavaScript 源代码.我相信它带有默认安装.

You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

这篇关于有什么方法可以在 Android 中使用 WebView 自动链接 URL 和电话号码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆