Android 隐藏、显示软键盘方法

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

imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘

隐藏软键盘的终极方法:

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);
        }
    }
}

那 SoftKeyboardUtil 第二个方法的 List<View> viewList 参数是什么, viewList 中需要放的是当前界面所有触发软键盘弹出的控件。 比如一个登陆界面, 有一个账号输入框和一个密码输入框, 需要隐藏键盘的时候, 就将两个输入框对象放在 viewList 中, 作为参数传到 hideSoftKeyboard 方法中即可。

 

如下方法会弹出的隐藏,隐藏的弹出

public static void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

详细见API:

\android-sdk\sources\android-26\android\view\inputmethod\InputMethodManager.java


 

Android 手动显示和隐藏软键盘

https://blog.csdn.net/changsimeng/article/details/72853760

1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 

2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘

3、调用隐藏系统默认的输入法

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this
.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  

//(WidgetSearchActivity是当前的Activity)  

 

4、获取输入法打开的状态

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值