[英]disable/enable soft keyboard for webview
@Override
public void onPageFinished(WebView view, String url) {
if (url.endWith("first.html")) {
webview.setFocusable(false);
} else {
webview.setFocusable(true);
}
}
At first page, I can disable soft keyboard, and enable it when I navigate to other pages. 在第一页,我可以禁用软键盘,并在导航到其他页面时启用它。
But when I back to the first page, the keyboard can't be disabled. 但是当我返回首页时,无法禁用键盘。
It's weird. 有点奇怪。
But if I press home key, then back to my app. 但是,如果按Home键,则返回到我的应用程序。 the keyboard would be disabled again.
键盘将再次被禁用。
Try this: 尝试这个:
protected void hideKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
@Override
public void onPageFinished(WebView view, String url) {
if (url.endWith("first.html")) {
webview.setFocusable(false);
hideKeyboard(view);
} else {
webview.setFocusable(true);
}
}
If this doesn't work there may be some other view gaining focus. 如果这不起作用,则可能会有其他观点获得关注。 You can use a method like this to work to find the view with focus and force the keyboard to be hidden:
您可以使用类似这样的方法来找到具有焦点的视图并强制隐藏键盘:
protected void hideKeyBoard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
I found my own answer, works perfectly. 我找到了自己的答案,效果很好。
@Override
public void onPageFinished(WebView view, String url) {
if (url.endWith("first.html")) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
// we have to clear focus that gain at other pages.
View currentFocus = getWindow().getCurrentFocus();
if (currentFocus != null) {
currentFocus.clearFocus();
}
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.