Why my webView doesn't show anything and automatically redirect me to in-built browser?
我想在对话框中显示我的网页内容。它可以是普通的
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> |
然后首先创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | class ShowJob : DialogFragment(), View.OnClickListener { var listPosition: Int? = 0 var offset: Int? = 0 lateinit var link: String val TAG = ShowJob::class.java.simpleName!! override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val bundle = arguments offset = bundle!!.getInt("jobOffset") link = bundle.getString("link") listPosition = bundle.getInt("listPosition", 0) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.show_job_scr,container,false) val webView = view.findViewById<WebView>(R.id.webView) webView.loadUrl(link) webView.webViewClient = HelloWebViewClient() WebView.setWebContentsDebuggingEnabled(false) return view } private inner class HelloWebViewClient : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent) return true } } override fun onClick(p0: View?) { } } |
它不显示任何内容并将我重定向到 Chrome 等内置浏览器。然后我读到有时更改根布局类型的类型会有所帮助,并将我的
的父片段中显示了
1 2 3 4 5 6 7 8 9 10 11 12 | val webDialog = Dialog(context) webDialog.setContentView(R.layout.show_job_scr) webDialog.setCancelable(true) val webView = webDialog.findViewById<WebView>(R.id.webView) !webView.isScrollbarFadingEnabled !webView.isHorizontalScrollBarEnabled webView.settings.javaScriptEnabled webView.clearCache(true) webView.loadUrl(url) webDialog.show() |
它还将我重定向到内置浏览器。我的问题在哪里,我该如何解决?我看到了这个例子,这里开发人员设法在对话框中显示内容。我看到了很多示例,并且到处有人设法在对话框中显示 Web 内容,但我在重定向方面遇到了一些问题。
1 2 3 4 5 6 7 8 9 10 11 12 | // this is the reason webView.webViewClient = HelloWebViewClient() private inner class HelloWebViewClient : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) startActivity(intent) return true } } |
我已经通过在这个问题上发布的解决方案解决了我的问题:
1 2 3 4 5 6 | webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { view.loadUrl(url) return false } } |
我删除了内部类并在
中添加了这些行