转自http://blog.youkuaiyun.com/jjmm2009/article/details/7967730
这几天在做自动弹出与隐藏软键盘,EditText在不同的使用环境下调用的方法是不一样的,找不到通用万能的方法,网上写的那些方法也只能作参考,不通用;这里总结一下我的心得,写出来分享给大家,希望对大家有帮助。
1.普通Activity中调用软键盘(下面两种方法都可以用):
方法一:
WindowManager.LayoutParams params = act.getWindow().getAttributes();
// 隐藏软键盘
act.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;
//显示软键盘
act.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
方法二:
//显示软键盘
mReasonEt.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mReasonEt, 0);
//隐藏软键盘
mReasonEt.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(mReasonEt.getWindowToken(), 0);
2.TabActivity中嵌套的子Activity中调用软键盘:
//显示软键盘
mReasonEt.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mReasonEt, 0);
//隐藏软键盘
mReasonEt.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(mReasonEt.getWindowToken(), 0);
在TabActivity中调用软键盘,默认会把底部Tab菜单推上去,导致界面显示和操作不方便,可以设置Activity的属性,让键盘直接覆盖底部tab菜单;如下:
androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan"
3.自定义Dialog中的EditText调用软键盘:
//显示软键盘
myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//显示键盘(这个方法针对三星9100使用)
warningDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//隐藏软键盘
myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// 隐藏软键盘(这个方法针对三星9100使用)
WindowManager.LayoutParams params = getWindow().getAttributes();
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;
4.捕获屏幕点击事件,隐藏输入法
getWindow().getDecorView().setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
return false;
}
});
调用系统键盘搜索
转自http://www.2cto.com/kf/201408/327967.html
如图所示,有时候为了布局美观,在搜索时没有搜索按钮,而是调用软件盘上的按钮。调用的实现只需要在XML在输入框中加入android:imeOptions="actionSearch",调用软键盘时,回车键就会显示搜索二字。
然后调用 OnEditorActionListener,不是OnKeyListener
searchText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId ==EditorInfo.IME_ACTION_SEARCH){
// 先隐藏键盘
((InputMethodManager) searchText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
getActivity()
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//跳转activity
Intent intent = new Intent();
intent.setClass(getActivity(), SearchResultActivity.class);
startActivity(intent);
return true;
}
return false;
}
});
在androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan"可以防止软键盘会把原来的界面挤上去的问题