package com.mytestapp;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.view.View;
|
import android.view.inputmethod.InputMethodManager;
|
import android.widget.EditText;
|
|
/**
|
* 软键盘工具类
|
* ·是否隐藏
|
* ·显示
|
* ·隐藏
|
*/
|
public class SoftInputUtil {
|
|
/**
|
* 判断键盘是否显示(当前editText是否在输入法活动状态,即是否正在接收软键盘输入)。
|
* @param editText 输入框
|
*/
|
public static boolean isSoftInputShowing(EditText editText){
|
return isSoftInputShowing(editText.getContext(), editText);
|
|
}
|
|
/**
|
* 判断键盘是否显示(当前view是否在输入法活动状态,即是否正在接收软键盘输入)。
|
* @param context 上下文
|
* @param view 当前聚焦的正在接收软件盘输入的View。
|
*/
|
public static boolean isSoftInputShowing(Context context, View view) {
|
boolean bool = false;
|
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
if (imm != null && imm.isActive(view)) {
|
bool = true;
|
}
|
return bool;
|
}
|
|
/**
|
* 判断键盘是否显示(是否有View是在输入法活动状态,即是否正有View正在接收软键盘输入)。
|
* @param context 上下文
|
*/
|
public static boolean isSoftInputShowing(Context context) {
|
boolean bool = false;
|
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
if (imm != null && imm.isActive()) {
|
bool = true;
|
}
|
return bool;
|
}
|
|
/**
|
* 显示键盘
|
* @param view 当前聚焦的正在接收软件盘输入的View,通常为EditText。
|
*/
|
public static void showSoftInput(View view) {
|
showSoftInput(view.getContext(), view);
|
}
|
|
/**
|
* 显示键盘
|
* @param context 上下文
|
* @param view 当前聚焦的正在接收软件盘输入的View,通常为EditText。
|
*/
|
public static void showSoftInput(Context context, View view) {
|
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
if (imm != null) {
|
//第二个参数为flags, 0 | InputMethodManager.SHOW_FORCED | InputMethodManager.SHOW_IMPLICIT
|
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
|
}
|
}
|
|
/**
|
* 隐藏软键盘
|
* @param activity
|
*/
|
public static void hideSoftInput(Activity activity) {
|
View view = activity.getWindow().peekDecorView();
|
if (view != null) {
|
hideSoftInput(activity, view);
|
}
|
}
|
|
public static void hideSoftInput(View view) {
|
if (view != null) {
|
hideSoftInput(view.getContext(), view);
|
}
|
}
|
|
/**
|
* 隐藏输入法
|
* @param context 上下文
|
* @param view 可以是任意已添加到window中的View(已添加到布局中的View)。
|
*/
|
public static void hideSoftInput(Context context, View view) {
|
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
|
if (imm != null && imm.isActive()) {
|
//第二个参数为flags, 0 | InputMethodManager.HIDE_IMPLICIT_ONLY | InputMethodManager.HIDE_NOT_ALWAYS
|
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
}
|
}
|
|
}
|