软键盘显示隐藏Util类

软键盘显示隐藏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();
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容