美文网首页Android
android关闭软键盘

android关闭软键盘

作者: 努力深耕Android的小透明 | 来源:发表于2018-01-23 11:00 被阅读3741次

涉及到软键盘必须用到系统服务的InputMethodManager,
具体的代码如下:

 public void hintKeyBoard() {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果window上view获取焦点 && view不为空
        if(imm.isActive()&&getCurrentFocus()!=null){
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken()!=null) {
                //表示软键盘窗口总是隐藏,除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }

getCurrentFocus()是拿到已经获得焦点的view
getCurrentFocus().getWindowToken()是拿到window上的token

相关文章

网友评论

    本文标题:android关闭软键盘

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