软键盘自动打开与关闭

来源:互联网 发布:画韦恩图的软件 编辑:程序博客网 时间:2024/05/08 14:54

Android软键盘有时候需要我们在EditText点击输入内容结束后自动关闭软键盘,或者某种情况自动打开软键盘,具体的操作代码如何,如下所示:

如果是点击完ET自动关闭软键盘可以写一个定时器在0.5秒后自动关闭软键盘

/**  * date: 2017/4/25 14:37  * autour: HelenChen * description: 键盘延迟显示隐藏*/private void inputManager() {    Timer timer = new Timer();    timer.schedule(new TimerTask()   {        public void run() {            hintKbOne();        }    }, 1000);/** * date: 2017/4/25 14:27 * autour: HelenChen * description: 此方法,如果显示则隐藏,如果隐藏则显示*/private void hintKbOne() {    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);    // 得到InputMethodManager的实例    if (imm.isActive()) {        // 如果开启        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);    }}/** * date: 2017/4/25 14:27 * autour: HelenChen * description: 此方法只是关闭软键盘*/private void hintKbTwo() {    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);    if(imm.isActive()&&getActivity().getCurrentFocus()!=null){        if (getActivity().getCurrentFocus().getWindowToken()!=null) {            imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);        }    }}

2 0
原创粉丝点击