如何在Android上使用JavaScript填写表单? [英] How can I fill out a form using Javascript on Android?

查看:173
本文介绍了如何在Android上使用JavaScript填写表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在开发一个填写表单的应用程序。我不知道如何做到这一点,除了我需要为此使用Javascript。

  @Override 
protected void onCreate这个代码加载了网页,但是并没有向表单输入任何值。 (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
工具栏工具栏=(工具栏)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

WebView myWebview =(WebView)findViewById(R.id.webview);
WebSettings webSettings = myWebview.getSettings();
webSettings.setJavaScriptEnabled(true);

myWebview.setWebViewClient(新的WebViewClient(){


public void onPageFinished(WebView视图,String url){
WebView myWebview =(WebView) findViewById(R.id.webview);

//隐藏加载图片
findViewById(R.id.progress).setVisibility(View.GONE);
//显示WebView
findViewById(R.id.webview).setVisibility(View.VISIBLE);

}
});
myWebview.loadUrl(https://www.panerabread.com/en-us/mypanera/registration-page.html);
myWebview.loadUrl(javascript:document.getElementById(\join_first_name \)。value = \Hello World \;);
}

以下是我的第二堂课。不知道这是干什么的,我从文档中获得了它。

  public class WebAppInterface {
Context mContext;

/ **实例化接口并设置上下文* /
WebAppInterface(Context c){
mContext = c;
}

}


解决方案

尝试将loadUrl(javascript ...)放入onPageFinished侦听器中,并在页面加载前调用它。


Alright, so I'm working on an app that will fill out forms. I have no idea how to do this, other than the fact that I need to use Javascript for this. This code loads the web page, but does not input any values to the form.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        WebView myWebview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        myWebview.setWebViewClient(new WebViewClient() {


            public void onPageFinished(WebView view, String url) {
                WebView myWebview = (WebView) findViewById(R.id.webview);

                //hide loading image
                findViewById(R.id.progress).setVisibility(View.GONE);
                //show WebView
                findViewById(R.id.webview).setVisibility(View.VISIBLE);

            }
        });
        myWebview.loadUrl("https://www.panerabread.com/en-us/mypanera/registration-page.html");
        myWebview.loadUrl("javascript:document.getElementById(\"join_first_name\").value=\"Hello World\";");
    }

Below is my second class. Not sure what this does, I got it from the documentation though.

    public class WebAppInterface {
        Context mContext;

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

    }

解决方案

Try putting the loadUrl("javascript..." into the onPageFinished listener. You're calling it before the page is loaded.

这篇关于如何在Android上使用JavaScript填写表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆