【问题标题】:disable/enable soft keyboard for webview禁用/启用 webview 的软键盘
【发布时间】:2017-05-26 06:04:42
【问题描述】:
@Override
public void onPageFinished(WebView view, String url) {
if (url.endWith("first.html")) {
webview.setFocusable(false);
} else {
webview.setFocusable(true);
}
}
在第一页,我可以禁用软键盘,并在导航到其他页面时启用它。
但是当我回到第一页时,键盘无法禁用。
这很奇怪。
但是如果我按主页键,然后回到我的应用程序。键盘将再次被禁用。
【问题讨论】:
标签:
android
webview
keyboard
【解决方案1】:
试试这个:
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);
}
}
如果这不起作用,则可能是其他一些视图获得焦点。您可以使用这样的方法来查找具有焦点的视图并强制隐藏键盘:
protected void hideKeyBoard() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
【解决方案2】:
我找到了自己的答案,效果很好。
@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);
}
}