博客
关于我
解决Android中WebView跳转到浏览器的问题
阅读量:348 次
发布时间:2019-03-04

本文共 4025 字,大约阅读时间需要 13 分钟。

用到了腾讯x5的webview 当然原生的也可以

//腾讯x5    implementation 'com.tencent.tbs.tbssdk:sdk:43939'
MyApplication->onCreate

新增  我的是kt语法,,具体就是创建map添加进去

// 在调用TBS初始化、创建WebView之前进行如下配置        // 在调用TBS初始化、创建WebView之前进行如下配置        val map = mapOf(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER to true,TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE to true)        QbSdk.initTbsSettings(map)

自定义webview

package com.anguomob.video.viewimport android.annotation.SuppressLintimport android.content.Contextimport android.util.AttributeSetimport com.tencent.smtt.sdk.WebSettingsimport com.tencent.smtt.sdk.WebViewimport com.tencent.smtt.sdk.WebViewClientclass X5WebView @SuppressLint("SetJavaScriptEnabled") constructor(    arg0: Context?,    arg1: AttributeSet?) :    WebView(arg0, arg1) {    private val client: WebViewClient = object : WebViewClient() {        /**         * 防止加载网页时调起系统浏览器         */        override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {            view.loadUrl(url)            return true        }    }    private fun initWebViewSettings() {        val webSetting: WebSettings = this.getSettings()        webSetting.setJavaScriptEnabled(true)        webSetting.setJavaScriptCanOpenWindowsAutomatically(true)        webSetting.setAllowFileAccess(true)        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS)        webSetting.setSupportZoom(true)        webSetting.setBuiltInZoomControls(true)        webSetting.setUseWideViewPort(true)        webSetting.setSupportMultipleWindows(true)        // webSetting.setLoadWithOverviewMode(true);        webSetting.setAppCacheEnabled(true)        // webSetting.setDatabaseEnabled(true);        webSetting.setDomStorageEnabled(true)        webSetting.setGeolocationEnabled(true)        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND)        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE)        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension        // settings 的设计    }    init {        this.setWebViewClient(client)        // this.setWebChromeClient(chromeClient);        // WebStorage webStorage = WebStorage.getInstance();        initWebViewSettings()        this.getView().setClickable(true)    }}

总结

核心是因为下面的client没有设置 所以才跳转到 了系统默认浏览器 可以直接用webweb.setVewViewClient去设置一下就可以了

private val client: WebViewClient = object : WebViewClient() {        /**         * 防止加载网页时调起系统浏览器         */        override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {            view.loadUrl(url)            return true        }    }
this.setWebViewClient(client)

使用

package com.anguomob.video.activityimport android.os.Bundleimport android.view.KeyEventimport android.view.Viewimport androidx.appcompat.widget.Toolbarimport com.anguomob.video.Rimport com.tencent.smtt.sdk.WebSettingsimport com.tencent.smtt.sdk.WebViewimport com.tencent.smtt.sdk.WebViewClientimport com.yilan.sdk.common.ui.BaseActivity/** * Create by: liuan * Create date: 2020-10-19 0019 * Describe: */class WebViewX5Acitivity : BaseActivity() {    lateinit var mWebView: WebView    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_x5_webview)        mWebView = findViewById
(R.id.forum_context); mWebView.loadUrl("https://www.anguomob.com") } // 5、覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法 override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { return if (keyCode == KeyEvent.KEYCODE_BACK) { if (mWebView.canGoBack()) { mWebView.goBack() //goBack()表示返回WebView的上一页面 true } else { finish() true } } else false }}

 

转载地址:http://orsr.baihongyu.com/

你可能感兴趣的文章
桌面图标的自动排列图标
查看>>
121 买卖股票的最佳时机(寻找数组中单调递增的序列中最小数字与最大数字--单调栈)
查看>>
第十一届蓝桥杯python组第二场省赛-数字三角形
查看>>
蓝桥杯四平方和(暴力)
查看>>
递归生成重复元素的全排列
查看>>
手机号码(数位dp-dfs)
查看>>
算法训练 Anagrams问题
查看>>
Linux-文件目录类常用指令3
查看>>
搜索查找类指令
查看>>
数字三角形的无返回值的深度优先搜索解法
查看>>
完全背包问题的简化思路
查看>>
Jquery添加元素
查看>>
Jquery使用需要下载的文件
查看>>
Spring中如何传递参数的问题
查看>>
Ajax中get方式url传递中文参数乱码的解决
查看>>
BST中某一层的所有节点(宽度优先搜索)
查看>>
广度优先搜索
查看>>
对于递归的理解
查看>>
二分查找(递归)
查看>>
猜字母
查看>>