禁用/启用webview的软键盘

时间:2017-05-26 06:04:42

标签: android webview keyboard

@Override
public void onPageFinished(WebView view, String url) {
    if (url.endWith("first.html")) {
        webview.setFocusable(false);
    } else {
        webview.setFocusable(true);
    }
}

在第一页,我可以禁用软键盘,并在导航到其他页面时启用它。

但是当我回到第一页时,键盘无法被禁用。

很奇怪。

但如果按主页键,则返回我的应用程序。键盘将再次被禁用。

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

我找到了自己的答案,效果很好。

@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);
    }
}
相关问题