美文网首页
Android软键盘隐藏及显示

Android软键盘隐藏及显示

作者: Android百晓生 | 来源:发表于2020-05-14 17:59 被阅读0次

一、页面初始化隐藏:
Manifest中增加:
<activity android:name="xxx.xx.xxActivity"
android:windowSoftInputMode="adjustPan|stateHidden"/>

二、强制隐藏:
(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);//强制显示
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘

三、弹出的隐藏,隐藏的弹出
public static void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

四、其它方法
public class SoftKeyboardUtil {

/**
 * 隐藏软键盘(只适用于Activity,不适用于Fragment)
 */
public static void hideSoftKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

/**
 * 隐藏软键盘(可用于Activity,Fragment)
 */
public static void hideSoftKeyboard(Context context, List<View> viewList) {
    if (viewList == null) return;

    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);

    for (View v : viewList) {
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

}

五、获取输入法状态
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen = imm.isActive();//isOpen若返回true,则表示输入法打开

相关文章

网友评论

      本文标题:Android软键盘隐藏及显示

      本文链接:https://www.haomeiwen.com/subject/hhlxohtx.html