用法:SoftKeyboardUtil.hideSoftKeyboard(LoginActivity.this);
代码如下:
import android.app.Activity; import android.content.Context; import android.view.View; import android.view.inputmethod.InputMethodManager; /** * * @Date 2020/7/13 0013 下午 5:03 */ public class SoftKeyboardUtil { /** * 隐藏软键盘(只适用于Activity,不适用于Fragment) */ public static void hideSoftKeyboard(Activity activity) { View view = activity.getCurrentFocus(); if (view != null) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } } /** * 隐藏软键盘(可用于Activity,Fragment) */ public static void hideSoftKeyboard(Context context, View... view) { if (view.length == 0) return; InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); for (View value : view) { inputMethodManager.hideSoftInputFromWindow(value.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } public static void hideKeyboard(View v) { InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive()) { imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0); } } }