博客
关于我
解决Android中WebView跳转到浏览器的问题
阅读量:353 次
发布时间: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/

你可能感兴趣的文章
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>