gpt4 book ai didi

android - Webview android目标='_blank'

转载 作者:行者123 更新时间:2023-12-05 07:41:51 25 4
gpt4 key购买 nike

我正在 Android Studio 中构建一个在 WebView 中显示页面的应用程序,但我似乎无法让 WebView 在同一个 WebView 中打开带有 target='_blank' 的页面。这能做到吗?

目前我有这个。因为现在当我点击带有 target='_blank' 的链接时没有任何反应如果我更改 onCreateWindow,我会在外部浏览器中打开链接,但我不希望发生这种情况。

我试过谷歌搜索但找不到解决方案。

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("http://test.dk");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportMultipleWindows(true);

myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
{

WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
//Context context = view.getContext();
//Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
//context.startActivity(browserIntent); // opens in external browser
myWebView.loadUrl(data); //don't work!
return false;
}
});

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}

}
return super.onKeyDown(keyCode, event);
}

// Use When the user clicks a link from a web page in your WebView
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

return false;
}
}

最佳答案

这对我有用。试试这个

 webView = (WebView) findViewById(R.id.career_view);
URL = "http://test.dk/";
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(URL); // Specify the URL to load when the application starts
webView.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
setTitle("Loading...");
setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 70)
setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new SSLTolerentWebViewClient());
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setInitialScale(1); // Set the initial zoom scale
webView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component
webView.getSettings().setUseWideViewPort(true);

创建私有(private)类(class)

private class SSLTolerentWebViewClient extends WebViewClient {
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {

AlertDialog.Builder builder = new AlertDialog.Builder(Activities.this);
AlertDialog alertDialog = builder.create();
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
}

message += " Do you want to continue anyway?";
alertDialog.setTitle("SSL Certificate Error");
alertDialog.setMessage(message);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Ignore SSL certificate errors
handler.proceed();
}
});

alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

handler.cancel();
}
});
alertDialog.show();
}
}

关于android - Webview android目标='_blank',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com