我们使用EditText编辑文本的时候,通过会使用软件键盘的自动显示,这样可以方便用户的使用。
那如何通过代码控制显示隐藏
第一种方法:
隐藏键盘
//通过InputMethodManager 对象 管理软键盘显示隐藏
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(etEditText.getWindowToken(), 0); //[backcolor=#f7f7f7][color=#666666]etEditText[/color][/backcolor] 是EditText对象
显示键盘:可以通过下面的方面重新恢复显示
调用的方法还是im.showSoftInput(etEditText,0);
复制代码
//
boolean showSoftInput(View view, int flags, ResultReceiver resultReceiver)
boolean showSoftInput(View view, int flags)
void showSoftInputFromInputMethod(IBinder token, int flags)
第二种方法:
隐藏键盘
protected void hideKeyboard() {
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocus = getCurrentFocus();
if (currentFocus != null) {
IBinder localIBinder = getCurrentFocus().getWindowToken();
localInputMethodManager.hideSoftInputFromWindow(localIBinder, 0);
}
}
更多知识点的学习,可以到迹涯网 http://www.jiyanet.com