主流网站宽度,网站建设功能介绍,seo教程最新,济南手机建站价格文章目录 Android 软键盘的显示和隐藏核心方法软键盘工具类点击空白区域隐藏软键盘方式一方式二 Android 软键盘的显示和隐藏
核心方法
显示软键盘#xff1a;
InputMethodManager#showSoftInput(View view, int flags)隐藏软键盘#xff1a;
InputMethodManager#hideSof… 文章目录 Android 软键盘的显示和隐藏核心方法软键盘工具类点击空白区域隐藏软键盘方式一方式二 Android 软键盘的显示和隐藏
核心方法
显示软键盘
InputMethodManager#showSoftInput(View view, int flags)隐藏软键盘
InputMethodManager#hideSoftInputFromWindow(IBinder windowToken, int flags)flags操作标志
flags0SHOW_IMPLICITSHOW_FORCED0YYYHIDE_IMPLICIT_ONLYNYNHIDE_NOT_ALWAYSYYN
先弹出软键盘再隐藏软键盘Y 表示软键盘隐藏N 表示软件不能隐藏。
SHOW_IMPLICIT隐式弹窗键盘表示弹出键盘不是由用户直接发起的键盘可能不会弹出。
SHOW_FORCED强制弹出键盘表示弹出键盘是用户直接发起的在用户请求收起键盘前软键盘会一直显示。
HIDE_IMPLICIT_ONLY只有通过 SHOW_IMPLICIT 弹出的键盘才能被隐藏。
HIDE_NOT_ALWAYS表示只要不是通过 SHOW_FORCED 弹出的键盘都会被隐藏。
调用 SHOW_FORCED 可以保证软键盘一定会弹出调用 0 可以保证软键盘一定会隐藏。
也可以简单粗暴的将两个方法中的 flag 都设为 0。
软键盘工具类
/*** 软键盘工具类*/
public class KeyboardUtils {/*** 显示软键盘** param editText*/public static void showSoftInput(EditText editText) {if (editText null)return;InputMethodManager imm (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);if (imm null)return;editText.setFocusable(true);editText.setFocusableInTouchMode(true);editText.requestFocus();imm.showSoftInput(editText, 0);}/*** 隐藏软键盘** param view*/public static void hideSoftInput(View view) {if (view null)return;InputMethodManager inputMethodManager (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);if (inputMethodManager null)return;inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);}/*** 隐藏软键盘** param editText*/public static void hideSoftInput(EditText editText) {if (editText null)return;InputMethodManager inputMethodManager (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);if (inputMethodManager null)return;editText.clearFocus();inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);}/*** 隐藏软键盘** param activity*/public static void hideSoftInput(Activity activity) {Window window activity.getWindow();View view window.getCurrentFocus();if (view null) {view window.getDecorView();}hideSoftInput(view);}/*** 软键盘切换*/public static void toggleSoftInput() {InputMethodManager imm (InputMethodManager) BaseApplication.getInstance().getSystemService(Context.INPUT_METHOD_SERVICE);if (imm null)return;imm.toggleSoftInput(0, 0);}}点击空白区域隐藏软键盘
方式一
public class BaseActivity extends AppCompatActivity {Overridepublic boolean onTouchEvent(MotionEvent event) {if (getCurrentFocus() ! null) {InputMethodManager mInputMethodManager (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);if (mInputMethodManager ! null) {return mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);}}return super.onTouchEvent(event);}
}方式二
public class BaseActivity extends AppCompatActivity {// Override
// public boolean onTouchEvent(MotionEvent event) {
// if (getCurrentFocus() ! null) {
// InputMethodManager mInputMethodManager (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// if (mInputMethodManager ! null) {
// return mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
// }
// }
// return super.onTouchEvent(event);
// }Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction() MotionEvent.ACTION_DOWN) {View view getCurrentFocus();if (isShouldHideKeyboard(view, ev)) {KeyboardUtils.hideSoftInput(view);}}return super.dispatchTouchEvent(ev);}/*** 是否隐藏软键盘*/private boolean isShouldHideKeyboard(View v, MotionEvent event) {if (v ! null v instanceof EditText) {int[] location {0, 0};v.getLocationInWindow(location);int left location[0];int top location[1];int right left v.getWidth();int bottom top v.getHeight();return !(event.getX() left event.getY() right event.getY() top event.getY() bottom);}return false;}
}