将数据从 java 类传递到 Web 视图 html

新手上路,请多包涵

我在我的 webView 中加载以下 html

https://mail-attachment.googleusercontent.com/attachment/?ui=2&ik=25c0c425c6&view=att&th=138db54ff27ad34b&attid=0.1&disp=inline&realattid=f_h5ahtmbe0&safe=1&zw&saduie=AG9B_P9YNooGjsk_jLefLptQ9q15&sadet=1343790299575&sads=-yBVsLKP_2mh7zMfYLCF7sL1u-w

现在我想要做的是填充来自我的 java 类变量的 html 中的文本框,然后自动点击提交。

但我不知道该怎么做。

任何想法将不胜感激。

原文由 User 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 211
1 个回答

首先,您的网址似乎不可用。

如果你想在 android 应用程序和你的网络应用程序/网页之间进行数据交换,你可以通过 javascript 实现。

以下是来自 Android 官方网站的示例:

像这样创建一个类:

 public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(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();
    }
}

在你的 WebView

 WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

在您的网页中:

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

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

如果您想将某些内容传递到您的网页,只需调用相应的 javascript 函数:

 String str = "xxx";
myWebView.loadUrl("javascript:xxx('"+str+"')");

这是参考:http: //developer.android.com/guide/webapps/webview.html

原文由 hungr 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题