600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 解决Android中WebView跳转到浏览器的问题

解决Android中WebView跳转到浏览器的问题

时间:2020-01-03 15:33:36

相关推荐

解决Android中WebView跳转到浏览器的问题

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

//腾讯x5implementation '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)

使用

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"><com.anguomob.video.view.X5WebViewandroid:id="@+id/forum_context"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="5dp"android:paddingRight="5dp" /></LinearLayout>

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.mon.ui.BaseActivity/*** Create by: liuan* Create date: -10-19 0019* Describe:*/class WebViewX5Acitivity : BaseActivity() {lateinit var mWebView: WebViewoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_x5_webview)mWebView = findViewById<WebView>(R.id.forum_context);mWebView.loadUrl("")}// 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}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。