简体   繁体   English

SearchView禁用自动键盘材质设计

[英]SearchView Disable auto keyboard Material Design

In onCreate() I set : onCreate()我设置了:

searchView.setOnQueryTextListener(this);
searchView.setQuery("example text", false);

In Material Design when I open the activity it automatically opens keyboard(without clicking on SearchView) 在Material Design中,当我打开活动时,它会自动打开键盘(无需单击SearchView)

But when I'm trying the same on non material design android device it fortunately does not automatically open the keyboard. 但是,当我在非材料设计的android设备上尝试相同操作时,幸运的是不会自动打开键盘。

So how to disable automatically opening of keyboard in onCreate for Material Design android devices? 那么如何在Material Design android设备的onCreate禁用自动打开键盘的功能呢?

Add this method and call it in your onCreate after your layout has been inflated. 添加此方法并在布局放大后在onCreate中调用它。

private void closeKeyboard() {
    View currentFocus = this.getCurrentFocus();
    if (currentFocus!=null){
        android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager)this.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
    }
    this.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM