KeyboardUtils

/**
* 软键盘相关辅助类
*/
public class KeyBoardUtils {

private KeyBoardUtils() {
throw new UnsupportedOperationException("cannot be instantiated");
}

/**
* 隐藏输入盘
*
* @param activity
*/
public static void collapseSoftInput(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
if (activity.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(activity
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}

/**
* 显示输入盘
*
* @param context
* @param inputText
*/
public static void showSoftInput(Context context, EditText inputText) {
if (inputText == null) {
return;
}

InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}

/**
* 隐藏输入盘
*
* @param context
* @param inputText
*/
public static void hideSoftInput(Context context, EditText inputText) {
if (inputText == null) {
return;
}

InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputText.getWindowToken(), 0);
}

/**
* 设置EditText的光标
*
* @param editText
*/
public static void setFocus(EditText editText) {
editText.requestFocus();

CharSequence text = editText.getText();
if (text instanceof Spannable) {
Selection.setSelection((Spannable) text, text.length());
}
}

/**
* 设置EditText的光标并选择文字
*
* @param editText
*/
public static void setFocusAndSelection(EditText editText) {
editText.requestFocus();

CharSequence text = editText.getText(http://www.amjmh.com);
if (text instanceof Spannable) {
Selection.selectAll((Spannable) text);
}
}
}
--------------------- 

posted @ 2019-08-09 19:24  李艳艳665  阅读(253)  评论(0编辑  收藏  举报