当前位置: 代码迷 >> 综合 >> android打开和关闭软件盘工具类
  详细解决方案

android打开和关闭软件盘工具类

热度:18   发布时间:2024-01-26 15:08:32.0

1、进入界面就打开软键盘

 /*** 打开软键盘** @param mEditText* @param mContext*/public static void openKeybord(View mEditText, Context mContext) {InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);}

2、关闭软键盘

 /*** 关闭软键盘** @param mEditText* @param mContext*/public static void closeKeybord(View mEditText, Context mContext) {InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);}

封装成工具类

public class KeyBoardUtil {/*** 打开软键盘** @param mEditText* @param mContext*/public static void openKeybord(View mEditText, Context mContext) {InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);}/*** 关闭软键盘** @param mEditText* @param mContext*/public static void closeKeybord(View mEditText, Context mContext) {InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);}public static void hideInputMethod(Activity activity) {InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);View currentFocus = activity.getCurrentFocus();if (currentFocus != null) {imm.hideSoftInputFromWindow(currentFocus.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}}public static void showInputMethod(Activity activity) {InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);View currentFocus = activity.getCurrentFocus();if (currentFocus != null) {imm.hideSoftInputFromWindow(currentFocus.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);imm.showSoftInput(currentFocus, InputMethodManager.SHOW_IMPLICIT);}}
}

 

  相关解决方案