备注:如果您想使用静态编程语言执行此操作,请使用:context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
静态编程语言语法
// Only runs if there is a view that is currently focusedthis.currentFocus?.let { view ->val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManagerimm?.hideSoftInputFromWindow(view.windowToken, 0)}
public void setEditTextFocus(boolean isFocused) {searchEditText.setCursorVisible(isFocused);searchEditText.setFocusable(isFocused);searchEditText.setFocusableInTouchMode(isFocused);
if (isFocused) {searchEditText.requestFocus();}}
然后,确保EditText的onFocus打开/关闭键盘:
searchEditText.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (v == searchEditText) {if (hasFocus) {// Open keyboard((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(searchEditText, InputMethodManager.SHOW_FORCED);} else {// Close keyboard((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);}}}});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {try {Method method = TextView.class.getMethod("setSoftInputShownOnFocus", boolean.class);method.invoke(mEditText, false);} catch (Exception e) {// Fallback to the second method}}
public View/RelativeLayout/so and so (Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.context = context;init();}
private void toggleKeyboard(){
if(keypadPager.getVisibility() == View.VISIBLE){Intent i = new Intent(this, MainActivity.class);i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);Bundle state = new Bundle();onSaveInstanceState(state);state.putBoolean(SHOW_KEYBOARD, true);i.putExtras(state);
startActivity(i);}else{Intent i = new Intent(this, MainActivity.class);i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);Bundle state = new Bundle();onSaveInstanceState(state);state.putBoolean(SHOW_KEYBOARD, false);i.putExtras(state);
startActivity(i);}}
public static void hideKeyboard(Activity activity) {InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);//Find the currently focused view, so we can grab the correct window token from it.View view = activity.getCurrentFocus();//If no view currently has focus, create a new one, just so we can grab a window token from itif (view == null) {view = new View(activity);}imm.hideSoftInputFromWindow(view.getWindowToken(), 0);}
private boolean isKeyboardVisible() {Rect r = new Rect();//r will be populated with the coordinates of your view that area still visible.mRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = mRootView.getRootView().getHeight() - (r.bottom - r.top);return heightDiff > 100; // if more than 100 pixels, its probably a keyboard...}
protected void showKeyboard() {if (isKeyboardVisible())return;InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if (getCurrentFocus() == null) {inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);} else {View view = getCurrentFocus();inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);}}
protected void hideKeyboard() {if (!isKeyboardVisible())return;InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);View view = getCurrentFocus();if (view == null) {if (inputMethodManager.isAcceptingText())inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);} else {if (view instanceof EditText)((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards buginputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}}
/*** This method is used to hide keyboard* @param activity*/public static void hideKeyboardFrom(Activity activity) {InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);}
alertDialog = dialogBuilder.create();alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//And only when everything is finished - let's bring up the window -alertDialog.show();
//Viola... keyboard is waiting for you open and ready...//Just don't forget to request focus for the needed view (i.e. EditText..)
val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager//Hide:imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);//Showimm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
<EditTextandroid:id="@+id/my_edit_text"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="Tap here to type"android:inputType="text" />
/*** If no window token is found, keyboard is checked using reflection to know if keyboard visibility toggle is needed** @param useReflection - whether to use reflection in case of no window token or not*/fun Fragment.hideKeyboard(context: Context = MainApp.instance, useReflection: Boolean = true) {val windowToken = view?.rootView?.windowTokenval imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManagerwindowToken?.let {imm.hideSoftInputFromWindow(windowToken, 0)} ?: run {if (useReflection) {try {if (getKeyboardHeight(imm) > 0) {imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)}} catch (exception: Exception) {Timber.e(exception)}}}}
fun getKeyboardHeight(imm: InputMethodManager): Int = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight").invoke(imm) as Int
private fun hideKeyboard(){val imm = activity!!.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManagerimm.hideSoftInputFromWindow(activity!!.currentFocus!!.windowToken, 0)}
fun View.hideKeyboard() {val imm = this.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManagerimm.hideSoftInputFromWindow(windowToken, 0)}
fun hideKeybord (){val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManagerif (inputManager.isAcceptingText){inputManager.hideSoftInputFromWindow(currentFocus.windowToken, 0)}
there are two ways to do so...
method 1:in manifest file
define the line **android:windowSoftInputMode="adjustPan|stateAlwaysHidden"** of code in your manifest.xml file as below...
<activityandroid:name="packagename.youactivityname"android:screenOrientation="portrait"android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />
Method 2 : in Activity or Java class
if(getCurrentFocus()!=null) {InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)`enter code here`;inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);}
fun View.showKeyboard() {this.requestFocus()val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManagerinputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)}
fun View.hideKeyboard() {val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManagerinputMethodManager.hideSoftInputFromWindow(windowToken, 0)}
fun hideKeyboard(activity: Activity?) {val inputManager: InputMethodManager? =activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as?InputMethodManager// check if no view has focus:val v = activity?.currentFocus ?: returninputManager?.hideSoftInputFromWindow(v.windowToken, 0)}
//In ActivityView v = this.getCurrentFocus();if (v != null) {InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);im.hideSoftInputFromWindow(view.getWindowToken(), 0);}
//In FragmentView v = getActivity().getCurrentFocus();if (v != null) {InputMethodManager im = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);im.hideSoftInputFromWindow(view.getWindowToken(), 0);}```
fun hideSoftKeyboard() {val view = activity?.currentFocusview?.let { v ->val imm =activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager // or contextimm.hideSoftInputFromWindow(v.windowToken, 0)}}
object Extensions {
fun View.hideKeyboard() {val inputMethodManager =context.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE)as InputMethodManagerinputMethodManager.hideSoftInputFromWindow(windowToken, 0)}}
在你需要隐藏键盘的fra的类的活动中,你可以像下面这样使用main布局id调用这个函数
//constraintEditLayout is my main view layout, if you are using other layout like relative or linear layouts you can call with that layout idconstraintEditLayout.hideKeyboard()
fun View.hideKeyboard() {val imm = ContextCompat.getSystemService(context, InputMethodManager::class.java) as InputMethodManagerimm.hideSoftInputFromWindow(windowToken, 0)}
val keyboardController = LocalSoftwareKeyboardController.current
//for showing the keyboardkeyboardController?.show()//for hiding the keyboardkeyboardController?.hide()