2017-05-26 77 views
0
@Override 
public void onPageFinished(WebView view, String url) { 
    if (url.endWith("first.html")) { 
     webview.setFocusable(false); 
    } else { 
     webview.setFocusable(true); 
    } 
} 

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

但是當我回到第一頁時,鍵盤不能被禁用。

這很奇怪。

但是,如果我按Home鍵,然後回到我的應用程序。鍵盤將被禁用。

回答

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