Mys*_*icϡ 8 android webview webviewclient
我面临一个奇怪的问题.
在我的应用程序中,我需要根据资源文件夹中的WebView中的单击按钮加载静态html文件.
现在在5个html文件中,一个html文件包含15个静态链接.这些链接需要在移动浏览器中将用户重定向到提到的URL.我target="_blank"在我的html文件中用于此目的如下.
<div class="lid"><a href="https://www.irctc.co.in/" target="_blank">Railway Reservation </a></div>
Run Code Online (Sandbox Code Playgroud)
现在,这在一个简单的示例应用程序中工作正常,WebView没有WebViewClient添加任何内容.
但我需要一个WebViewClient我的其他功能.那时候target="_blank"完全被忽略了.并且URL在WebView本身中打开.
我找到了一个解决方法,我可以使用shouldOverrideUrlLoading如下:
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if(url.equalsIgnoreCase("https://www.irctc.co.in/"))
{
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
else
return super.shouldOverrideUrlLoading(view, url);
}
});
Run Code Online (Sandbox Code Playgroud)
所以这是在默认浏览器中打开该特定URL.
基本上我的问题是:
target="_blank"我们使用时为什么被忽略WebViewClient?这个问题还有其他解决方法吗?因为我有15个链接,我需要比较.我无法在新浏览器中加载所有网址,因为有一些链接需要在同一个WebView中打开.
我有一个解决方法的想法。将应在新窗口中打开的所有链接替换为自定义架构。然后你就可以自己处理了。
为了最大限度地减少中断,还设置了自定义主题并处理所有配置更改:
<activity android:name="com.example.LinkHandler"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="your.link.handler.schema"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
然后在链接处理程序中,您可以使用 读取 url getIntent().getData()。
另请记住,您应该处理 http 和 https,我在上面的简短示例中跳过了这一点。
下面是一个如何重写 url 的 JavaScript 示例:
<activity android:name="com.example.LinkHandler"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="your.link.handler.schema"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
for(var i=0; i<document.links.length; i++) {
if(document.links[i].target == "_blank") {
document.links[i].href = "your.schema.for."+document.links[i].href;
}
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4973 次 |
| 最近记录: |