导读:
这里是一个关于Keyboard开启或关闭的工具类,方便平常直接拷贝用,就不赘述了
代码:
public final class KeyboardToggleUtils {
/**
* 动态显示软键盘
*
* @param activity activity
*/
public static void showSoftInput(Activity activity) {
View view = activity.getCurrentFocus();
if (view ==
null) view =
new View(activity);
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm ==
null)
return;
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
/**
* 动态显示软键盘
*
* @param view 视图
*/
public static void showSoftInput(View view) {
view.setFocusable(
true);
view.setFocusableInTouchMode(
true);
view.requestFocus();
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm ==
null)
return;
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
/**
* 动态隐藏软键盘
*
* @param activity activity
*/
public static void hideSoftInput(Activity activity) {
View view = activity.getCurrentFocus();
if (view ==
null) view =
new View(activity);
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm ==
null)
return;
imm.hideSoftInputFromWindow(view.getWindowToken(),
0);
}
/**
* 动态隐藏软键盘
*
* @param view 视图
*/
public static void hideSoftInput(View view) {
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm ==
null)
return;
imm.hideSoftInputFromWindow(view.getWindowToken(),
0);
}
/**
* 切换键盘显示与否状态
*/
public static void toggleSoftInput() {
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm ==
null)
return;
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
0);
}
}