In Android, how can I set the value of a edit box in WebView using Javascript

后端 未结 4 1481
醉酒成梦
醉酒成梦 2021-02-09 14:27

I understand how to set the value of a edit box in WebView, and in a browser on PC with Javascript.

It is basically first find out the ID of the edit box (text), then s

相关标签:
4条回答
  • 2021-02-09 15:02

    Use the setText() function of TextView as in:

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);
    

    Where 'message' is a String variable, this will put the value of message into the textView box replacing anything that was in there previously. I got this example from the code at the bottom of this page https://developer.android.com/training/basics/firstapp/starting-activity.html

    EditText uses the same setText() method

    0 讨论(0)
  • 2021-02-09 15:09

    The problem is solved. It was because of the way I call javascript in java code.

    code like

    mWebView.loadUrl("javascript: [your javascript code]");
    

    sometimes work, but it doesn't work in all the case. At least it doesn't work for the following case when I tried to set the textbox in a webview:

    mWebView.loadUrl("javascript: document.getElementById('xxxxxxxx').value= 'test'");
    

    but it should work if you call the javascript as a function:

    mWebView.loadUrl("javascript: (function() {document.getElementById('xxxxxxxx').value= 'test';}) ();" );
    

    That fixed my problem. Hope it is helpful.

    0 讨论(0)
  • 2021-02-09 15:16

    This worked perfectly for me:

    String ABC="50";
    
    myWebView.loadUrl("javascript: (function(){document.getElementById('countLiveSteps').value ='" + ABC + "';})();");
    
    0 讨论(0)
  • 2021-02-09 15:21

    I would imagine you are running the command before the page is loaded or it cannot resolve document itself. You should write this code as a function included in your document and then have android do:

    mWebView.loadUrl("javascript: myFunction('test')");
    

    Or better still, use @JavascriptInterface

    0 讨论(0)
提交回复
热议问题