您的位置:首页 > 其它

安卓软键盘的显示与隐藏

2017-06-29 10:32 162 查看
1: 软键盘的显示

当前布局必须已经完成加载,如果还未绘制完成,则showSoftInput()方法不起作用。如果要再布局文件加载后就显示软键盘,可以通过postDelayed的方式来延迟执行showSoftInput()。延迟时间不能太短,一般要在50ms以上。

// 显示软键盘
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
etSMS.requestFocus();
imm.showSoftInput(view, 0);
}
}
}, 100);


2:软键盘的隐藏

public void hideSoftInputWindow() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context
.INPUT_METHOD_SERVICE);
if (this.getCurrentFocus() != null) {
if (this.getCurrentFocus().getWindowToken() != null) {// 点击空白位置 隐藏软键盘
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: