Home  >  Article  >  Java  >  How to show and hide the soft keyboard in Android (manual)

How to show and hide the soft keyboard in Android (manual)

高洛峰
高洛峰Original
2017-01-07 11:48:081497browse

In Android development, there is often a need to hide the keyboard after completing a certain operation, that is, to prevent the soft keyboard in Android from displaying. Today, I would like to share with you how to use code to hide and display the Android software disk.

Android 显示和隐藏软键盘的方法(手动)

1. Method 1 (If the input method is already displayed on the window, it will be hidden, otherwise it will be displayed)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

2. Method 2 (view is View that accepts soft keyboard input, SHOW_FORCED means forced display)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘

3. Call the hidden system default input method

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)

4. Get the status of the input method being opened

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开

The above is a method for displaying and hiding the soft keyboard on Android (manually). I hope it will be helpful to everyone. For more related articles on how to display and hide the soft keyboard on Android (manually), please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn