editText.setText(string)不起作用,但textView.setText(string)始终有效,奇怪的区别

huangapple 未分类评论46阅读模式
英文:

editText.setText(string) does NOT work, but textView.setText (string) always works, strange difference

问题

所以我希望 editText.setText(str) 的效果与 textView.setText(str) 相同。

我从 WebView 发送一个字符串到 Android 的 @JavascriptInterface。奇怪的是,只有在 WebView 中点击发送按钮并且屏幕键盘弹出/激活时,EditText 才会执行 setText(string)

如果您在键盘下面看不到输入字段,则:

  1. 我关闭了屏幕键盘,
  2. 在 WebView 中点击发送按钮,
  3. 再次点击任何输入字段(或者在 Android 输入中),以弹出屏幕键盘,
  4. 此时字符串将显示在从 WebView 接收的 @JavascriptInterface 中的 EditText 中。

有趣的是,如果我在 TextView 中显示/接收字符串,则无需考虑任何屏幕键盘状态或点击。

我已经尝试过以下方法,但没有成功:

  • 使用定时任务在500毫秒后将字符串发送到 EditText,
  • 尝试调用外部函数,并在 EditText 之前使用 toast 显示消息字符串 - 这样消息总是会到达,但只有在 WebView 中的键盘仍然弹出时,EditText 才会显示。

我尝试发送点击事件到 WebView,但是在这种情况下键盘不会弹出。

@JavascriptInterface
public void showMessageInEditText(String str) {

     int test = 1;
     // EditText   不正常工作
     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   总是正常工作:
     if(3==test){  TextView textView = (TextView) findViewById(R.id.txt_id2);
         textView.setText(str);  }
}
/*
奇怪的区别:

textView.setText(str);  // test=3          总是正常工作

editText.setText(str);  // test=1 test=2   仅在以下情况下工作:
    屏幕键盘弹出/激活,
    WebView 中的输入激活,
    并且在此期间准确地点击了发送按钮,
    屏幕键盘弹出

*/

我更新了程序,尝试发送到另一个 Activity2,然后它就正常工作了。

英文:

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
    
    */

editText.setText(string)不起作用,但textView.setText(string)始终有效,奇怪的区别

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

editText.setText(string)不起作用,但textView.setText(string)始终有效,奇怪的区别

huangapple
  • 本文由 发表于 2020年7月27日 15:10:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63110333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定