显示软键盘
//显示小键盘
public void showSoftKeyboard(View view, Context mContext) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
隐藏软键盘
//隐藏小键盘
public void hintKeyBoard() {
//拿到InputMethodManager
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//如果window上view获取焦点 && view不为空
if (imm.isActive() && getCurrentFocus() != null) {
//拿到view的token 不为空
if (getCurrentFocus().getWindowToken() != null) {
//表示软键盘窗口总是隐藏,除非开始时以SHOW_FORCED显示。
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
搭配EditText 使用的使用结合获取焦点
mMyEd.setText(""); //置空
mMyEd.setVisibility(View.VISIBLE);
mMyEd.requestFocus(); //获取焦点
showSoftKeyboard(mMyEd, MainActivity.this); //呼出键盘
网友评论