Android中软键盘的管理主要是通过InputMethodManager来完成的,
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
使用方法:
import android.app.Activity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by Administrator on 2017/4/4.
* 软键盘工具类
*/
public class KeyboardUtil {
/**
* 显示软键盘,当布局加载完成后调用,否则无效
* @param context
* @param focusView:必须为EditText或者其子类并且获得焦点,并且是VISIBLE
*/
public static void showSoftInput(Context context, EditText focusView){
InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
focusView.requestFocus();
inputMethodManager.showSoftInput(focusView,0);
}
}
/**
* 隐藏软键盘,当布局加载完成后调用,否则无效
* @param context
*/
public static void hideSoftInput(Context context){
InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.hideSoftInputFromWindow(((Activity)context).getWindow().getDecorView().getWindowToken(),0);
}
}
/**
* 切换软键盘状态(隐藏-显示或显示-隐藏),当布局加载完成后调用,否则无效
* @param context
*/
public static void toggleSoftInput(Context context){
InputMethodManager inputMethodManager= (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(0,0);
}
}
}
参考博客: Android手动显示和隐藏软键盘