Why is Android WebView refusing user input?

前端 未结 16 1811
半阙折子戏
半阙折子戏 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:37

    Turns out that it was apparently the WebView not having focus that was the issue.

    I discovered that using the arrow keys to get focus on the textboxes caused them to work, so I theorised that there was an issue somewhere with something not having focus, most likely the WebView not having focus. Sure enough, adding the following line seemed to fix the problem:

    webView.requestFocus(View.FOCUS_DOWN);
    

    I'm still at a loss to explain exactly why the issue occurred in the first place - the textboxes should work whether they receive focus from being tapped upon or through being "arrowed" to - but at least I have a solution that appears to work.

    Thanks for your input wf.

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

    I faced with similar problem, that text inputs doesn't work on webview. When you focus on text input it shows keyboard, but you cannot type any characters.

    After a long search for solution, I found that this fixes the problem:

    body {
        -webkit-transform: translate3d(0,0,0);
        -moz-transform: translate3d(0,0,0);
        -ms-transform: translate3d(0,0,0);
        -o-transform: translate3d(0,0,0);
        transform: translate3d(0,0,0);
    }
    

    As I understood, this rule makes some devices run their hardware acceleration. More information about translate3d(0,0,0) here.

    On the other hand, according to this article it is not recommended to use GPU acceleration everywhere.

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

    in my context (webview containing an external URL), this fragment works:

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

    the error is because at every touch the webview was losing the focus!

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

    I don't know my case is exactly same as yours,

    I found that add one css line can solve problem.

    I just add

    input{
        -webkit-user-select: text;
    }
    

    to my css, then problem solved!

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

    If we are dealing with EditText in WebView, then v.requestFocusFromTouch () will not work if we have to USER_AGENT written Mozilla / 5.0 (X11; Linux i686) and will work fine if Mozilla / 5.0 (X11; Linux x86_64).

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

    These are most have

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.requestFocus(View.FOCUS_DOWN);
    

    and if still not working then use one alternative below

    Alternative 1

    webview.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;
            }
        });
    

    Alternative 2

    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    v.requestFocusFromTouch();  
                    break;
            }               
            return false;
        }
    
    });
    
    0 讨论(0)
提交回复
热议问题