【问题标题】:Is there a way of triggering the back button through a button click in a webpage? [closed]有没有办法通过点击网页中的按钮来触发后退按钮? [关闭]
【发布时间】:2019-12-11 10:48:29
【问题描述】:

我想在网页中提供一个按钮。单击它后,用户必须被重定向到应用程序,他从中打开网页的应用程序。我的想法是在用户单击网页中的按钮后触发 android 后退按钮。这是一种可能的方式还是有任何其他方式正在做。

【问题讨论】:

  • 这是android的默认行为吧?
  • 是的,你需要使用 JavaScript 接口,或者捕获按钮应该打开的链接
  • 我不希望用户按下 android 后退按钮。我的用例是我将要求用户在重定向到我的网页后上传图片。上传图片后点击确定我必须重定向他直接到应用程序。
  • @AntonisRadz 你能简要解释一下我如何使用 javascript 界面做到这一点。我不想使用捕获链接。

标签: android web-applications


【解决方案1】:

这将为在 WebView 中运行的 JavaScript 创建一个名为 Android 的界面。此时,您的 Web 应用程序可以访问 WebAppInterface 类。例如,下面是一些 HTML 和 JavaScript,当用户单击按钮时,它们会使用新界面创建一个 toast 消息:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

您可以在您的 Android 应用中包含以下类:

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

    【解决方案2】:

    WebView与app之间的通信需要使用JavascriptInterface

    Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多