edittext的弹起和隐藏,获取焦点

/**
 * EditText获取焦点并显示软键盘
 */
public static void showSoftInputFromWindow(Activity activity, EditText editText) {
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText,0);
}

/**
 * EditText 失去焦点并隐藏软键盘
 */
public static void hideSoftInputFromWindow(Activity activity, EditText editText) {
    editText.setFocusable(false);
    editText.setFocusableInTouchMode(false);
    editText.clearFocus();
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText,0);
}

//第二种方式隐藏键盘

/**
 * 获取InputMethodManager,隐藏软键盘
 * @param token 是editText.getWindowToken()
 * @param parentView 是布局最外层的view
 */
public static void hideKeyboard(IBinder token, View parentView, Context context) {
    if (token != null) {
        parentView.setFocusable(true);
        parentView.setFocusableInTouchMode(true);
        parentView.requestFocus();
        InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_28674511/article/details/83010162