首页 > 移动平台 > 详细

Android 强制软键盘关闭

时间:2015-06-08 18:58:30      阅读:341      评论:0      收藏:0      [点我收藏+]

  在Android开发过程中,有时候我们会有强制关闭软键盘的需求。比如说:现在有一个文本编辑框(testEt)和一个按钮(testBtn),我们现在点击文本编辑框testEd,这时会弹出软键盘,然后我们点击按钮testBtn,此时软键盘还是保持了打开的状态...问题来了,我们想要的结果是软键盘消失。(testBtn只是我随便举的一个例子,也可以使别的控件例如下拉框、可点击的图片、自定义空间等等)

  下面提供两种方法解决:

  一、这种方法只是关闭软键盘: 

  在按钮testBtn调用以下方法hideKeyboard():

  private void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive() && this.getCurrentFocus() != null) {
      if (this.getCurrentFocus().getWindowToken() != null) {
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
      }
    }
  }

  二、这种方法会线判断软键盘的状态,如果时关闭状态则打开,如果是打开状态则关闭:

  在按钮testBtn调用以下方法hideKeyboard():

  private void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive()) {
      if (this.getCurrentFocus().getWindowToken() != null) {
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
      }
    }
  }

 

  

技术分享
技术分享

Android 强制软键盘关闭

原文:http://www.cnblogs.com/guohb/p/4561527.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!