开发者

Why is Android WebView refusing user input?

开发者 https://www.devze.com 2022-12-16 21:50 出处:网络
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 textboxes, but typing in th

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 textboxes, but typing in them will not work. That is, they definitely have input focus (they have the orange focus highlight box and a flashing cursor), but typing in them does absolutely nothing. I'm not certain, but I think maybe the form buttons are also playing up - they appear to be simply refreshing the pa开发者_如何学编程ge, rather than submitting the form.

Just to be clear, although I'm particularly interested in getting Facebook running, I'm sure that this isn't a Facebook issue since other websites (Google, etc) also display the same behavior.

Does anyone have any ideas what might be the issue?


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.


@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.


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;
    }

});


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!


I'm sorry to say that none of this worked for me. I guess this depends on your context. In my case, the problem occurred within a html pop up (absolute div). All the other input field were doing ok.

Breaking down the problem I found out it's a bug in the webview. Fixed positioned divs containing other fixed/absolute position divs break input fields in the page. Hence a rule I worded for you (don't hesitate to reformulate):

If in your page you've got a fixed positioned div with none-default positioned nested divs (i.e. absolute, fixed, etc.), any of the following absolute positioned div in your page containing input fields will get the unexpected behaviour.

So, put your input fields at the top of the page (if you can) or avoid absolute divs nested in other fixed/absolute divs. That should help.

I posted something about it on my blog if you need more explanations and potential workarounds: http://java-cerise.blogspot.com/2012/02/android-web-view-inputfields-refuse-to.html


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.


Not sure if it's the problem since other websites also display the same behavior, but have you enabled javascript? A WebView does not enable it by default.

WebView webView = (WebView)findViewById(R.id.yourWebView);
webView.getSettings().setJavaScriptEnabled(true);


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


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


I encountered this problem because I am displaying a splash image while the webview is loading the first page. I am setting the webview to View.VISIBLE in onPageFinished, however despite being able to focus text boxes you can't type into them. I too can confirm that the fix to set the webview requestFocus to View.FOCUS_DOWN works.

NB. If I set the webview to View.VISIBLE in onResume the problem does not present itself but in my case the splash image disapears too soon.


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.


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.


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.


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!


By default, In Android, Webview allow user input with basic configurations:-

android:focusable="true"
android:focusableInTouchMode="true"

& enabled JavaScript but make sure there is not any activity/fragment level focusability blockage, in my case

android:descendantFocusability="blocksDescendants"

just removed this, worked for me. So cross-check such cases & fix them accordingly.


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).


It also happened in my project. I tried all above methods, none of them can effective. Finally, I solved this problem by use a dialog theme activity replaced the native Dialog. Hope this can help someone!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号