【问题标题】:(Android) Javascript that is realized twice on WebView(Android) 在 WebView 上实现两次的 Javascript
【发布时间】:2022-02-01 14:04:01
【问题描述】:

我想通过 Webview 更改 GoogleForm 中的文本,但是该操作发生了两次。

在文本框中,单词出现了两次,就像 javascript 发生了两次一样。

你有什么想法吗?谢谢

public class QuestionnaireFragment extends Fragment  {
//Lancement de l'activité
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_questionnaire, container, false);

    WebView view = (WebView) rootView.findViewById(R.id.webview_questionnaire);
    String url = "LINK GOOGLE FORMS";
    WebSettings webSettings = view.getSettings();
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);

    view.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String js ="javascript:document.getElementsByClassName(\"quantumWizTextinputPaperinputInput exportInput\")[0].focus(); document.execCommand('insertText', false, '" + "WORD"+ "');";

            view.loadUrl(js);

        }
         });
    return rootView;
}

}

编辑: 我通过以下方式更改字符串:

String js= "javascript:document.getElementsByClassName(\"quantumWizTextinputPaperinputInput exportInput\")[0].value=\"WORD\";";

但另一个问题:

【问题讨论】:

  • 其实你也不是很清楚。能否请您添加问题的屏幕截图?
  • 是的,我当然添加了
  • 您是否尝试将String js ="javascript:document.getElementsByClassName(\"quantumWizTextinputPaperinputInput exportInput\")[0].focus(); document.execCommand('insertText', false, '" + "WORD"+ "');"; 替换为String js ="javascript:document.getElementsByClassName(\"quantumWizTextinputPaperinputInput exportInput\")[0].focus(); document.execCommand('insertText', true, '" + "WORD"+ "');";
  • 我尝试过,但还是一样:/ 4 小时后我尝试删除这个“故障”(我是新手)
  • 不,它总是出现这个错误:/

标签: javascript java android webview


【解决方案1】:

在案例 1 中,该词重复了两次。在案例 2 中,提示没有进行。所以,如果我们使用这两行代码,我们就可以解决问题。所以只要这样做,它就会起作用:

view.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            String js ="javascript:document.getElementsByClassName(\"quantumWizTextinputPaperinputInput exportInput\")[0].focus(); document.execCommand('insertText', false, '" + ""+ "');";

            view.loadUrl(js);
            
            String js2 = "javascript:document.getElementsByClassName(\"quantumWizTextinputPaperinputInput exportInput\")[0].value=\"WORD\";";
            view.loadUrl(js2);


        }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多