Android - webview在执行javascript代码时显示不同的行为

时间:2015-12-02 14:01:58

标签: javascript android android-webview

我正在通过语音在项目阅读用户输入中工作并在网站上写,我通过jsoup阅读所有网站输入ID

WebView webView = (WebView) findViewById(R.id.webview);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webView.clearCache(true);
            webView.clearHistory();
            webView.setWebChromeClient(new WebChromeClient());
            webView.setWebViewClient(new WebViewClient() {

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    new ParseURl().execute(new String[]{url});
                }
            });

            webView.loadUrl(passed_site);

读取所有输入ID的方法很好用

private class ParseURl extends AsyncTask<String, Void, Boolean> {


        @Override
        protected Boolean doInBackground(String... params) {

            try {
                Document doc = Jsoup.connect(params[0]).get();
                Elements inputs = doc.select("input[type=text]");
                for (Element ele : inputs) {
                    listID.add(ele.attr("id"));
                }

            } catch (IOException e) {
                e.printStackTrace();


       }


                if(listID.size()>0){
                    return true;
                }
                return false;
            }

 @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            if(result){
                readMyVoice();
            }else {
                Toast.makeText(WebPage.this , "your site dont have input to fill" , Toast.LENGTH_LONG).show();
            }
        }
    }

如果网站包含输入,则通过用户输入开始写入

 private void readMyVoice() {
        Intent voicerecogize = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        voicerecogize.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
        voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        voicerecogize.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ar-SA");
        startActivityForResult(voicerecogize, RESULT_SPEECH);
    }



 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RESULT_SPEECH && requestCode == RESULT_OK) ;
        {
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (results.size() > 0 ) {

                StartFillSiteForm(listID.get(count),results.get(0));
                count++;
                if(listID.size() == count){
                    return;
                }else{
                    readMyVoice();
                }
            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

这里是问题,当这个方法尝试将用户输入写入网站时,所有工作正常它打开新页面空如下屏幕截图,应该在网站输入中写这个

private void StartFillSiteForm(String id , String value){

        webView.loadUrl("javascript: document.getElementById('"+id+"').value='" + value + "';");

    }

测试网站谷歌

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该在Android Activity中启用javascript

@SuppressLint("SetJavaScriptEnabled") 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);
}