为了以后项目更方便的控制软键盘的显示和隐藏,我写了一个简单的工具类。
public class InputMethodUtils {
/**
* 显示软键盘
*/
public static void showInputMethod(View view) {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
/**
* 隐藏软键盘
*/
public static void hindInputMethod(Context context) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 多少时间后显示软键盘
*/
public static void showInputMethod(final View view, long delayMillis) {
// 显示输入法
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
InputMethodUtils.showInputMethod(view);
}
}, delayMillis);
}
/**
* 关闭软键盘
* @param context
* @param view
*/
public static void closeInputMethod(Context context, View view){
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0) ;
}
}