Why is Android WebView refusing user input?

前端 未结 16 1810
半阙折子戏
半阙折子戏 2020-11-28 08:12

I\'m developing an Android application that uses a WebView to display the login page for Facebook. The page loads beautifully, and I\'m able to select the username/password

相关标签:
16条回答
  • 2020-11-28 08:25

    I found a problem that might be different, but it sounds similar. In the android 2.3 native browser, if you have fixed position elements on the page, it sometimes breaks select boxes.

    A workaround for this problem for 2.3 devices is to never allow empty child elements for a fixed position parent. Thus,

    <div style="position:fixed">
      <div>
        <span>not empty</span>
        <span></span>
      </div>
    </div>
    

    would become

    <div style="position:fixed">
      <div>
        <span>not empty</span>
        <span>&nbsp;</span>
      </div>
    </div>
    

    This fixed my problem.

    Don't know if this was the issue for your problem, and this is over a year after-the-fact, but figured I would share, as I'm currently dealing with another fixed postion issue on an app, and I haven't found a workaround for it.

    0 讨论(0)
  • 2020-11-28 08:27

    It was happened to me loading a page in 4.1.2 and 4.2.2 and after one day of searching, I found the answer here (comment #18)

    Quote from the original post:

    Some of the various web pages I was rendering with WebView didn't fit properly into the WebView and as a result a div (or some other html component ) were being invisibly laid over the input fields. Although the input fields appeared selected when touched, they would not allow text input (even if i did manage to get the soft keyboard up using the track ball).

    So the solution.

    webview.getSettings().setUseWideViewPort(true);
    

    This won't completely stop the issue, but makes the view more like a PC browser which sites are designed for. Less change of the overlay.

    0 讨论(0)
  • 2020-11-28 08:28

    @Mac Does this single line :

    webView.requestFocus(View.FOCUS_DOWN);
    

    solved your problem ? I didn't in my case ,but this does :

    mWebView.setOnTouchListener(new View.OnTouchListener() { 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
               switch (event.getAction()) { 
                   case MotionEvent.ACTION_DOWN: 
                   case MotionEvent.ACTION_UP: 
                       if (!v.hasFocus()) { 
                           v.requestFocus(); 
                       } 
                       break; 
               } 
               return false; 
            }
    });
    

    just for your referrence.

    0 讨论(0)
  • 2020-11-28 08:29

    In my case TabHost from a previous fragment in back stack was stealing the focus from WebView input on every key press. Here's how I fixed it:

    tabHost.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
      @Override
      public void onViewDetachedFromWindow(View v) {}
    
      @Override
      public void onViewAttachedToWindow(View v) {
        tabHost.getViewTreeObserver().removeOnTouchModeChangeListener(tabHost);
      }
    });
    

    See https://code.google.com/p/android/issues/detail?id=2516 for more on the topic.

    0 讨论(0)
  • 2020-11-28 08:30

    I tried all the other solutions posted here, none of which worked.

    Instead, I extended the WebView and overrode the InputConnection which forced KeyEvents to dispatch.

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
      return new BaseInputConnection(this, false);
    }
    
    0 讨论(0)
  • 2020-11-28 08:31

    I spend a lot of time to solve this problem. Finally, I realized that it is not about WebView. In my case, I have a webview inside dialog. I want to handle back pressed button to cancel dialog and finish activity as well. And I wrote code below:

    private void setOnBackPressed() {
        this.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                    activity.finish();
                }
                return true;
            }
        });
    }
    

    So, when I use the keyboard, this method reject all key presses somehow and then it doesn't appear on the text fields.

    I only changed the return value to false. So, it worked properly.

    Hope it helps!!

    PS: this method is inside my dialog class that extends Dialog class

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