从 Activity 类调用 Web 视图中的 JavaScript 函数

发布于 2024-12-12 05:37:21 字数 1570 浏览 0 评论 0原文

编辑:这个剪辑最终成功了。我之前曾尝试过此操作,但实际上我的 javascript 遇到了范围界定问题。我在 document.onready 中定义了 appBack() 。只需将该函数的作用域重新调整为 *window.*appBack = function()。生意做完了。希望这对某人有帮助。

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        WebView.loadUrl("javascript:appBack()");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

在 webview 应用程序中,后退导航是使用自定义 JS 函数 appBack() 处理的,我一直在尝试找出一种方法来拦截 android 物理后退按钮并调用该 javascript 函数。这是我的活动文件。它非常基本,设置一个网络视图并侦听后退按钮单击。目前,当用户单击此物理后退按钮时,它会运行 mWebView.goBack()。这是我想执行 javascript:appBack() 的地方

package com.stgeorgeplc.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;


public class StGeorgePLCliteActivity extends Activity {
    /** Called when the activity is first created. */
    WebView mWebView;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);

        mWebView.loadUrl("file:///android_asset/www/index.html");
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    } 
}

Edit: This snipped worked in the end. I had been trying this before but was actually experiencing a scoping issue with my javascript. I had appBack() defined in document.onready. Simply re-scoping that function to *window.*appBack = function(). Done the business. Hope this helps someone.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        WebView.loadUrl("javascript:appBack()");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Within a webview application the back navigation is handled with a custom JS function appBack() I've been trying to figure out a way to intercept the android physical back button and call that javascript function instead. Here is my activity file. Its very basic, sets up a webview and listens for the back button click. Currently when the user clicks this physical back button it runs mWebView.goBack(). Which is where I would like to perform javascript:appBack()

package com.stgeorgeplc.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;


public class StGeorgePLCliteActivity extends Activity {
    /** Called when the activity is first created. */
    WebView mWebView;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);

        mWebView.loadUrl("file:///android_asset/www/index.html");
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文