一、设置软键盘的“完成”按键
要将软键盘的可变按键设置为“完成”,只要在EditText布局里加上这句话就可以了。
android:imeOptions="actionDone"
如果你要将可变按键设置为“搜索”,那么还要设置为单行。
android:imeOptions="actionSearch"
android:singleLine="true"
二、点击空白处,收起软键盘
在需要点击空白收回软件盘的Activity中重写onTouchEvent方法即可。
//---------------------------------点击空白处收回软键盘-----------------------------------------
@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
return super.onTouchEvent(event);
}
//----------------------------------------------------------------------------------------------
三、手动收回软键盘
想在特定时候使用代码手动回收软键盘,只要添加如下代码即可
因为我是在Fragment中收回,所以要先获得相应的Activity。
//----------------------------------收起软键盘--------------------------------------------------------
UsingMain usingMain = (UsingMain)getActivity();
InputMethodManager imm = (InputMethodManager)usingMain.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(usingMain.getWindow().getDecorView().getWindowToken(),0);
}
//-----------------------------------------------------------------------------------------------------
实际效果: