赞
踩
- import android.os.Bundle
- import androidx.appcompat.app.AppCompatActivity
- import com.tencent.smtt.sdk.WebView
- import com.tencent.smtt.sdk.WebViewClient
-
- class MainActivity : AppCompatActivity() {
-
- private lateinit var webView: WebView
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
-
- webView = findViewById(R.id.webView)
- webView.settings.javaScriptEnabled = true // 启用JavaScript
- webView.webViewClient = object : WebViewClient() {
- override fun onPageFinished(view: WebView?, url: String?) {
- super.onPageFinished(view, url)
- // 页面加载完成后,执行JavaScript来滚动到指定的div
- scrollToDiv("targetDiv")
- }
- }
-
- webView.loadUrl("https://www.example.com")
- }
-
- private fun scrollToDiv(divId: String) {
- // 执行JavaScript,滚动到指定的div
- webView.evaluateJavascript("document.getElementById('$divId').scrollIntoView();", null)
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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"
- android:orientation="vertical"
- tools:context=".MainActivity">
-
- <com.tencent.smtt.sdk.WebView
- android:id="@+id/webView"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
-
- </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。