簡體   English   中英

Kotlin:將本地 html js 加載到 android WebView

[英]Kotlin: loading local html js into android WebView

所以我有一個 react-native 項目,但決定離開它,這與 airbnb 轉向原生開發(java/kotlin)的原因相同。 我的大部分經驗是 web 的東西,所以除了 hello world 之外沒有移動原生體驗。

簡而言之。 現在,我想使用 kotlin 並想通過將所有代碼加載到 WebView 中來挽救我的大部分代碼,然后隨着我獲得更多知識,可能會更原生地構建。 (可能每個活動/屏幕有 1 個 WebView 組件)。 而且我可能需要轉換我的 react-native 代碼以對 web 做出反應,但我想我已經知道了。

我的挑戰是在 js 中調用/引用本機功能(例如相機、本地存儲)來調用 kotlin function 或類似的東西,但我認為有辦法做到這一點,但我在 Z4FBA3BC02B72EE9A7678 中沒有看到這樣的例子米掙扎。

概括:

  • 將本地 html/js 文件加載到 WebView

  • 從 WebView 調用/訪問本機功能(例如相機、本地存儲)

到目前為止..activity_main.xml

 <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

MainActivity.kt

package com.example.kotlinwebview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView.settings.javaScriptEnabled = true
        webView.settings.setSupportZoom(false)

        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
            }
        }
    }
}

應用程序/src/main/assets/test.js

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>JavaScript Interface</title>
</head>
<body>
    <h1>This is HTML</h1>

    <script language="javascript">
       function someFunction() {
           ...
       }

       function someFunction2() {
           ...
       }

       function callFromActivity(msg){
           ...
       }
    </script>

</body>
</html>

如果你們能幫我做第一個,那就太酷了。

class MainActivity : AppCompatActivity() {

    // this is globar variable
    val FILECHOOSER_RESULTCODE = 1001
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView.settings.javaScriptEnabled = true
        webView.settings.setSupportZoom(false)

        webView.webChromeClient = object : WebChromeClient() {

            override fun onShowFileChooser(
                webView: WebView,
                filePathCallback: ValueCallback<Array<Uri>>,
                fileChooserParams: FileChooserParams
            ): Boolean {

                val contentSelectionIntent = Intent(Intent.ACTION_GET_CONTENT)
                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE)
                contentSelectionIntent.type = "*/*"
                val intentArray: Array<Intent?> = arrayOfNulls(0)

                val chooserIntent = Intent(Intent.ACTION_CHOOSER)
                chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent)
                chooserIntent.putExtra(Intent.EXTRA_TITLE, "File Chooser")
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray)
                startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE)
                return true
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == FILECHOOSER_RESULTCODE) {

            val result = if (intent == null || resultCode != RESULT_OK) null else data?.data
            result?.let {
                val uriArray: Array<Uri> = arrayOf(it)
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM