1. 点击空白处隐藏键盘:
在activity中:
private InputMethodManager manager ;
manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
/**
* Click on the blank space to hide the keyboard
* @param event
* @return
*/
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null) {
manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return super.onTouchEvent(event);
}
2.点击某个按钮隐藏键盘
//If the keyboard display status and then hide the keyboard, otherwise cancelled
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
else{
// to do
}
3.打开Activity时,不自动显示键盘
打开Activity时,不自动显示(弹出)虚拟键盘
在AndroidManifest.xml文件中<activity>标签中添加属性
android:windowSoftInputMode="stateHidden|adjustResize"
如果要显示就更改属性
android:windowSoftInputMode="stateVisible|adjustResize"