【问题标题】:How to import my main app classes into my imported library module?如何将我的主要应用程序类导入到我导入的库模块中?
【发布时间】:2021-09-05 09:08:33
【问题描述】:

所以我正在创建一个具有 WebView 以在 html 中创建注释的应用程序。为此,我使用了一个名为 Rich Text Editor 的 GitHub 库(我将它放在我的应用项目文件夹中)

现在的问题是我想从我的主应用程序文件夹中调用我的函数。我怎么做?我想这样做的原因是因为我正在从我在其中一个类中使用“@JavascriptInterface”创建的 WebView 中检测到 Button onclick。当用户单击按钮时,我需要获取按钮中的值并将其用于主应用程序文件夹中的其他功能。 谁能帮帮我?

这是我的主应用文件夹中的 VideoViewingFragment

    fun startsomething(vidTimestamp: Float){
    youTubePlayerView.getYouTubePlayerWhenReady(object: YouTubePlayerCallback{
          override fun onYouTubePlayer(youTubePlayer: YouTubePlayer) {
                                youTubePlayer.seekTo(vidTimestamp)}})}

这是我制作并放入富文本编辑器库中的 WebInterface 类:

class WebAppInterface(var mContext: Context) {
@JavascriptInterface
fun getString(text: String) {
//get text from the button click
// call the function from VideoViewingFragment
// This is where i want to call my functions from my activity in my other main app folder
}

这是从 .js 文件调用以调用 getStringFromButtonClick(在我的富文本编辑器库模块中)的 JavaScript 函数。每次点击都会调用这个函数。只有在用户创建了一个包含他们想要放入其中的任何文本的按钮之后......并且在它被点击之后,它才会被调用。

function myFunction(text){
         window.Android.showToast(text);}

我知道这是一种非常糟糕的技术,因为它会导致代码注入。但这仅供我自己使用。

【问题讨论】:

    标签: javascript java android kotlin import


    【解决方案1】:

    试试下面的代码,它会对你有所帮助。

    class TestActivity : AppCompatActivity(), WebViewFragment.WebViewCallback {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        
        //Make sure your webViewFragment calling from this activity 
    
    }
    
    override fun onButtonClick(text: String) {
        toast(text)
    }
    }
    
    
    //This is your webview fragment
    class WebViewFragment : Fragment() {
    private lateinit var webViewCallback: WebViewCallback
    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is WebViewCallback) this.webViewCallback = context
    }
    
    @JavascriptInterface
    fun getString(text: String) {
     //get text from the button click
     this.webViewCallback.onButtonClick(text)
     // call the function from VideoViewingFragment
     // This is where i want to call my functions from my activity in my other main app folder
    }
    
    
    interface WebViewCallback {
        fun onButtonClick(text: String)
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2020-12-14
      • 1970-01-01
      • 2017-07-03
      相关资源
      最近更新 更多