软键盘显示隐藏Util类
SoftkeyboardUtil.java
一个显示隐藏软键盘工具类
package ...;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class SoftKeyboardUtil {
/**
* 弹出内容为空的EditText软键盘
*
* @param context
* @param editText
*/
public static void showEmptyInput(Context context, EditText editText) {
if (editText != null && TextUtils.isEmpty(editText.getText())) {
editText.requestFocus();
showInputMethod(context, editText);
}
}
/**
* 弹出键盘
*
* @param context
* @param view 焦点view
*/
public static void showInputMethod(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, 0);
}
}
/**
* 隐藏键盘
*
* @param context
*/
public static void hideSoftKeyboard(Activity context) {
try {
if (context.getCurrentFocus() == null || context.getCurrentFocus().getWindowToken() == null)
return;
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 隐藏键盘
*
* @param context
*/
public static void hideSoftKeyboard(Context context, View view) {
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}