WebView 內的連結,預設會用系統瀏覽器開啟新視窗。
要改成在 WebView 內開啟連結,可設定 setWebViewClient。
找到的資料,是說要覆寫 setWebViewClient 內的 shouldOverrideUrlLoading() 方法,改寫網頁載入的處理方式,
但我測試只加 webview.setWebViewClient(new WebViewClient()) 似乎就可以,不過這邊還是採取改寫的方式。
webview = (WebView) findViewById(R.id.webview); webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { String url = request.getUrl().toString(); view.loadUrl(url); return true; //return super.shouldOverrideUrlLoading(view, request); } });
[連結 target 為 _blank 的連結,改成用系統瀏覽器開啟新視窗]
以上處理後,所有連結都會在 WebView 內開啟。
如果想讓 target 為開新視窗的連結,用系統瀏覽器開啟新視窗,
除了 setSupportMultipleWindows 設置為 true,
還須修改 setWebChromeClient 的 onCreateWindow() 方法,
當連結為開新視窗,會經由該方法處理。
webSettings = webview.getSettings(); webSettings.setSupportMultipleWindows(true);//設定可開啟多視窗 webview.setWebChromeClient(new WebChromeClient() { @Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, android.os.Message resultMsg) { //view:發起新窗口請求的 WebView //isDialog:true,新窗口是對話框。false,一個全尺寸窗口。 //isUserGesture:true,用戶觸發的,例如點擊連接 //resultMsg:創建新 WebView 後要發送的消息。resultMsg.obj是一個 WebView.WebViewTransport 物件 Log.i("觀察 isDialog", Boolean.toString(isDialog)); Log.i("觀察 isUserGesture", Boolean.toString(isUserGesture)); WebView newWebView = new WebView(MainActivity.this); WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj; transport.setWebView(newWebView); resultMsg.sendToTarget(); return true; } });
其他:
有找到另一種處理方式,基本可用,但我經由 JavaSript 開啟新視窗時,APP 會發生 crashwebSettings.setSupportMultipleWindows(true); webview.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); return false; } });
參考:
- https://www.codeprj.com/zh/blog/4089d61.html
淺談WebView在新窗口瀏覽網頁(setSupportMultipleWindows()與onCreateWindow()關系) - 碼上快樂 - https://stackoverflow.com/questions/5069901/overriding-android-webchromeclients-oncreatewindow-method-results-in-sigsegv
Overriding Android WebChromeClient's onCreateWindow method results in SIGSEGV - Stack Overflow - https://mlog.club/article/1334683
需要信息到Android Webview window.open()和window.close() | 码农俱乐部 - Golang中国 - Go语言中文社区 - https://stackoverflow.com/questions/27009995/android-webview-links-to-same-window-with-target-blank-to-open-new-window/27010225#27010225
html - Android WebView links to same window with target=_blank to open new window - Stack Overflow - https://stackoverflow.com/questions/18187714/android-open-target-blank-links-in-webview-with-external-browser/23431369
Android - Open target _blank links in WebView with external browser - Stack Overflow - https://vimsky.com/zh-tw/examples/detail/java-method-android.webkit.WebView.WebViewTransport.html
Java WebView.WebViewTransport方法代碼示例 - 純淨天空 - https://stackoverflow.com/questions/15368885/android-webview-determine-a-target-blank
javascript - Android WebView: Determine <a> target= "_ blank" - Stack Overflow - https://www.796t.com/post/M21jbzQ=.html
android:在我的webview中開啟彈出視窗 - ANDROID _程式人 - https://www.twblogs.net/a/5d4060a6bd9eee5174230c9c
Android WebView攔截iframe標籤內部跳轉 - 台部落 - https://www.cnblogs.com/ufreedom/p/4229590.html
浅谈WebView在新窗口浏览网页(setSupportMultipleWindows()与onCreateWindow()关系) - UFreedom - 博客园 - https://stackoverflow.com/questions/2835556/whats-the-difference-between-setwebviewclient-vs-setwebchromeclient
android - What's the difference between setWebViewClient vs. setWebChromeClient? - Stack Overflow - https://www.itread01.com/content/1549860687.html
WebView使用詳解之WebChromeClient的常用事件監聽 - IT閱讀
沒有留言:
張貼留言