为什么 Android WebView 拒绝用户输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2083909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is Android WebView refusing user input?
提问by Mac
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 page, rather than submitting the form.
我正在开发一个使用 WebView 显示 Facebook 登录页面的 Android 应用程序。页面加载精美,我可以选择用户名/密码文本框,但输入它们不起作用。也就是说,它们肯定具有输入焦点(它们具有橙色焦点突出显示框和闪烁的光标),但是在它们中输入绝对没有任何作用。我不确定,但我认为也许表单按钮也在播放 - 它们似乎只是刷新页面,而不是提交表单。
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.
需要明确的是,虽然我对让 Facebook 运行特别感兴趣,但我确信这不是 Facebook 的问题,因为其他网站(谷歌等)也显示相同的行为。
Does anyone have any ideas what might be the issue?
有没有人有任何想法可能是什么问题?
回答by Mac
Turns out that it was apparently the WebView not having focus that was the issue.
事实证明,问题显然是 WebView 没有焦点。
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 没有焦点。果然,添加以下行似乎解决了问题:
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.
感谢您的输入 wf。
回答by DiveInto
@Mac Does this single line :
@Mac 是否有这一行:
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.
仅供参考。
回答by touchchandra
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
备选方案 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
备选方案 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;
}
});
回答by newbeeep
I don't know my case is exactly same as yours,
我不知道我的情况和你的完全一样,
I found that add one css line can solve problem.
我发现添加一行 css 可以解决问题。
I just add
我只是添加
input{
-webkit-user-select: text;
}
to my css, then problem solved!
到我的 css,然后问题解决了!
回答by twingocerise
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.
我很遗憾地说这些都不适合我。我想这取决于你的上下文。就我而言,问题发生在 html 弹出窗口(绝对 div)中。所有其他输入字段都正常。
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.
分解问题我发现它是 webview 中的一个错误。包含其他固定/绝对位置 div 的固定定位 div 会中断页面中的输入字段。因此,我为您制定了一条规则(不要犹豫重新制定):
如果在您的页面中有一个固定定位的 div 和非默认定位的嵌套 div(即绝对、固定等),则以下任何绝对在包含输入字段的页面中定位 div 将出现意外行为。
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.
因此,将您的输入字段放在页面顶部(如果可以)或避免嵌套在其他固定/绝对 div 中的绝对 div。那应该有帮助。
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
如果您需要更多解释和潜在的解决方法,我在我的博客上发布了一些关于它的内容:http: //java-cerise.blogspot.com/2012/02/android-web-view-inputfields-refuse-to.html
回答by Maxim Kachurovskiy
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
,后堆栈中的前一个片段在每次按键时从 WebView 输入中窃取焦点。这是我修复它的方法:
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=2516for more on the topic.
有关该主题的更多信息,请参阅https://code.google.com/p/android/issues/detail?id=2516。
回答by sembozdemir
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:
我花了很多时间来解决这个问题。最后,我意识到这与 WebView 无关。就我而言,我在对话框中有一个 webview。我想处理后退按钮以取消对话框并完成活动。我在下面写了代码:
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.
我只将返回值更改为 false。所以,它工作正常。
Hope it helps!!
希望能帮助到你!!
PS: this method is inside my dialog class that extends Dialog class
PS:此方法在我的对话框类中,它扩展了 Dialog 类
回答by afathman
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.
相反,我扩展了 WebView 并覆盖了强制 KeyEvents 调度的 InputConnection。
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new BaseInputConnection(this, false);
}
回答by wf.
Not sure if it's the problem since other websites also display the same behavior, but have you enabled javascript? A WebViewdoes not enable it by default.
不确定这是否是问题,因为其他网站也显示相同的行为,但是您是否启用了 javascript?一个WebView中默认不启用它。
WebView webView = (WebView)findViewById(R.id.yourWebView);
webView.getSettings().setJavaScriptEnabled(true);
回答by jhad
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.
我遇到这个问题是因为我在 webview 加载第一页时显示了一个启动图像。我在 onPageFinished 中将 webview 设置为 View.VISIBLE,但是尽管能够聚焦文本框,但您无法输入它们。我也可以确认将 webview requestFocus 设置为 View.FOCUS_DOWN 的修复程序有效。
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.
注意。如果我在 onResume 中将 webview 设置为 View.VISIBLE,则问题本身不会出现,但在我的情况下,启动图像很快就会消失。