public class SoftKeyBoardUtil {
//打开软键盘 editText为对应打开的输入框
public static void openKeyBoard(final Context context, final EditText editText) {
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
Timer timer = new Timer();
//对于刚跳到一个新的界面就要弹出软键盘的情况,可能由于界面未加载完全而无法弹出软键盘。此时应该适当的延迟弹出软键盘时间如300毫秒(保证界面的数据加载完成)。
timer.schedule(new TimerTask() {
public void run() {
InputMethodManager inputManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}, 300);
}
//关闭软键盘
public static void closeKeyBoard(final Context context, final EditText editText) {
InputMethodManager inputManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
//切换键盘的开启与隐藏
public static void toggle(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
//判断键盘是否打开
public static boolean isKeyBoardOpen(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.isActive();//isOpen若返回true,则表示输入法打开
}
}
判断是否打开了软键盘 自动打开软键盘
最新推荐文章于 2018-12-24 10:47:00 发布