gpt4 book ai didi

android - 如何在应用程序 android Kotlin 中打开 webview 链接

转载 作者:行者123 更新时间:2023-12-02 12:21:31 25 4
gpt4 key购买 nike

我面临一个问题,当我单击 webview 中的任何链接时,它会自动打开 chrome 浏览器并在那里打开链接

我在这里粘贴主要的 Activity 类(class)。

class MainActivity : AppCompatActivity() {

private val url = "https://stackoverflow.com/"
private lateinit var webView: WebView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
// Get the web view settings instance
val settings = webView.settings

// Enable java script in web view
settings.javaScriptEnabled = true

// Enable and setup web view cache
settings.setAppCacheEnabled(true)
settings.cacheMode = WebSettings.LOAD_DEFAULT
settings.setAppCachePath(cacheDir.path)


// Enable zooming in web view
settings.setSupportZoom(false)
settings.builtInZoomControls = false
settings.displayZoomControls = false

// Zoom web view text
settings.textZoom = 100

// Enable disable images in web view
settings.blockNetworkImage = false
// Whether the WebView should load image resources
settings.loadsImagesAutomatically = true


// More web view settings
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
settings.safeBrowsingEnabled = true // api 26
}
//settings.pluginState = WebSettings.PluginState.ON
settings.useWideViewPort = true
settings.loadWithOverviewMode = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.mediaPlaybackRequiresUserGesture = false


// More optional settings, you can enable it by yourself
settings.domStorageEnabled = true
settings.setSupportMultipleWindows(true)
settings.loadWithOverviewMode = true
settings.allowContentAccess = true
settings.setGeolocationEnabled(true)
settings.allowUniversalAccessFromFileURLs = true
settings.allowFileAccess = true

// WebView settings
webView.fitsSystemWindows = true


/*
if SDK version is greater of 19 then activate hardware acceleration
otherwise activate software acceleration
*/
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)


// Set web view chrome client
webView.webChromeClient = object: WebChromeClient(){
override fun onProgressChanged(view: WebView, newProgress: Int) {
progress_bar.progress = newProgress

}
}
webView.loadUrl(url);


fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (url.contains("stackoverflow.com")) {
view.loadUrl(url)
} else {
val i = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(i)
}
return true
}
}


// Method to show app exit dialog
private fun showAppExitDialog() {
val builder = AlertDialog.Builder(this)

builder.setTitle("Please confirm")
builder.setMessage("No back history found, want to exit the app?")
builder.setCancelable(true)

builder.setPositiveButton("Yes") { _, _ ->

super@MainActivity.onBackPressed()
}

builder.setNegativeButton("No") { _, _ ->
// Do something when want to stay in the app
toast("thank you.")
}

// Create the alert dialog using alert dialog builder
val dialog = builder.create()

// Finally, display the dialog when user press back button
dialog.show()
}

}


fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

当我点击链接时。 chome 浏览器正在打开。我是android开发的新手,无法处理这种情况。请帮忙。

最佳答案

试试这个,我已经用内部 WebViewClient 替换了 ChromeViewClient

引用:How to load an URL inside a WebView using Android Kotlin?

class MainActivity : AppCompatActivity() {

private val url = "https://stackoverflow.com/"
private lateinit var webView: WebView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webview)
// Get the web view settings instance
val settings = webView.settings

// Enable java script in web view
settings.javaScriptEnabled = true

// Enable and setup web view cache
settings.setAppCacheEnabled(true)
settings.cacheMode = WebSettings.LOAD_DEFAULT
settings.setAppCachePath(cacheDir.path)


// Enable zooming in web view
settings.setSupportZoom(false)
settings.builtInZoomControls = false
settings.displayZoomControls = false

// Zoom web view text
settings.textZoom = 100

// Enable disable images in web view
settings.blockNetworkImage = false
// Whether the WebView should load image resources
settings.loadsImagesAutomatically = true


// More web view settings
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
settings.safeBrowsingEnabled = true // api 26
}
//settings.pluginState = WebSettings.PluginState.ON
settings.useWideViewPort = true
settings.loadWithOverviewMode = true
settings.javaScriptCanOpenWindowsAutomatically = true
settings.mediaPlaybackRequiresUserGesture = false


// More optional settings, you can enable it by yourself
settings.domStorageEnabled = true
settings.setSupportMultipleWindows(true)
settings.loadWithOverviewMode = true
settings.allowContentAccess = true
settings.setGeolocationEnabled(true)
settings.allowUniversalAccessFromFileURLs = true
settings.allowFileAccess = true

// WebView settings
webView.fitsSystemWindows = true


/*
if SDK version is greater of 19 then activate hardware acceleration
otherwise activate software acceleration
*/
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)


// Set web view client
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
return true
}
}
webView.loadUrl(url);


fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (url.contains("stackoverflow.com")) {
view.loadUrl(url)
} else {
val i = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(i)
}
return true
}
}


// Method to show app exit dialog
private fun showAppExitDialog() {
val builder = AlertDialog.Builder(this)

builder.setTitle("Please confirm")
builder.setMessage("No back history found, want to exit the app?")
builder.setCancelable(true)

builder.setPositiveButton("Yes") { _, _ ->

super@MainActivity.onBackPressed()
}

builder.setNegativeButton("No") { _, _ ->
// Do something when want to stay in the app
toast("thank you.")
}

// Create the alert dialog using alert dialog builder
val dialog = builder.create()

// Finally, display the dialog when user press back button
dialog.show()
}

}


fun Context.toast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

关于android - 如何在应用程序 android Kotlin 中打开 webview 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60641759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com