2

So I would that editText.setText (str) works the same as textView.setText (str)

I'm sending a string from WebView to Android @JavascriptInterface. Strangely, EditText only makes setText (string) when the send button is clicked in the WebView as long as the onscreen keyboard is popped up / active.

If you don't see the input field below the keyboard, then:

  1. I close the onscreen keybord,
  2. click send button in WebView,
  3. click again in any input field (or also in android Input) so that the onscreen keybord pops up,
  4. at this moment the string is displayed in the EditText in @JavascriptInterface, received from WebView.

Funny if I show / receive the string in a TextView it works without any onsceen keyboard status, or click click.

what can I do, I have already tried

a runnable scheduler to send the string to EditText 500 ms later,

and tried to call external functions, and displayed the message string with toast before EditText - so the message always arrives - only EditText shows which only if the keyboard is still popped up in the WebView.

try to send a click event to WebView , but in this focus the keyboard do not popup

    @JavascriptInterface
    public void showMessageInEditText(String str) {

         int test = 1;
         // EditText   not happy
         if(1==test){  EditText editText = (EditText) findViewById(R.id.edit_txt_id);
            editText.setText(str);  }

         if(2==test){  TextView editText = (TextView) findViewById(R.id.edit_txt_id);
             editText.setText(str);  }
         
         // TextView   always works:
         if(3==test){  TextView textView = (TextView) findViewById(R.id.txt_id2);
             textView.setText(str);  }
    }
    /*
    a strange difference:
    
    textView.setText(str);  // test=3          always works
    
    editText.setText(str);  // test=1 test=2   only works if 
        the onscreen keyboard is popped up / active, 
        for input in the WebView active, 
        and the send button is clicked exactly during this time, 
        the onscreen keyboard is popped up
    
    */

WebViev to Android java

I updated program , try send to another Activity2 and then it is ok

send to second Activity is ok

And
  • 55
  • 2
  • 7
  • As far as i know EditText.setText() does not accept String as argument, i wonder why are you not having an error this way... Just in case, can you try this: editText.setText(Editable.Factory.getInstance().newEditable(str)); – Rinat Diushenov Jul 27 '20 at 07:20
  • nothing yet // schade (de) – And Jul 27 '20 at 07:33
  • what? I didn't get it :) – Rinat Diushenov Jul 27 '20 at 07:35
  • IIRC, JavascriptInterface methods are not invoked on the main UI thread but you need to be on the main UI thread to modify UI widgets reliably. Run the setText() calls on the main thread. – laalto Jul 27 '20 at 09:58

0 Answers0