Kotlin WebView - 防止在新 Chrome 浏览器 Window 中打开
Kotlin WebView - Prevent Opening In New Chrome Browser Window
我已将以下代码插入 MainActivity.kt 的空白处 activity。
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private val url = "http://www.yahoo.com/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the web view settings instance
val setting = webview.settings;
// Enable java script in web view
setting.javaScriptEnabled = true
webview.loadUrl(url)
}
}
我还在 activity_main.xml 中插入了一个 ID 为 @+id/webview 的 WebView 组件。加载应用程序时,屏幕会重定向到加载 Yahoo 页面的 chrome 浏览器。如何在应用程序中维护浏览器?
我尝试在 URL 加载之前添加 webview.setWebViewRenderProcessClient = webViewClient()
,但是在编译代码时出现未解决的引用错误。
编辑
XML如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我还添加了 <uses-permission android:name="android.permission.INTERNET"/>
到 AndroidManifest.xml 文件。
实现WebView
的webClient
并在调用webView.loadUrl = "#someURL"
之前设置它。
示例:
webview.webViewClient = WebViewClient()
有关 WebView
的更多详细信息,请查看 here
我已将以下代码插入 MainActivity.kt 的空白处 activity。
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private val url = "http://www.yahoo.com/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Get the web view settings instance
val setting = webview.settings;
// Enable java script in web view
setting.javaScriptEnabled = true
webview.loadUrl(url)
}
}
我还在 activity_main.xml 中插入了一个 ID 为 @+id/webview 的 WebView 组件。加载应用程序时,屏幕会重定向到加载 Yahoo 页面的 chrome 浏览器。如何在应用程序中维护浏览器?
我尝试在 URL 加载之前添加 webview.setWebViewRenderProcessClient = webViewClient()
,但是在编译代码时出现未解决的引用错误。
编辑
XML如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我还添加了 <uses-permission android:name="android.permission.INTERNET"/>
到 AndroidManifest.xml 文件。
实现WebView
的webClient
并在调用webView.loadUrl = "#someURL"
之前设置它。
示例:
webview.webViewClient = WebViewClient()
有关 WebView
的更多详细信息,请查看 here