簡體   English   中英

禁用/啟用網絡視圖軟鍵盤

[英]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);
    }
}

在第一頁,我可以禁用軟鍵盤,並在導航到其他頁面時啟用它。

但是當我返回首頁時,無法禁用鍵盤。

有點奇怪。

但是,如果按Home鍵,則返回到我的應用程序。 鍵盤將再次被禁用。

嘗試這個:

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

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

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM