Android开发之如何隐藏软键盘

在Activity文件中加入下面的函数,实现点击空白处隐藏键盘:

 1      @Override

 2      public boolean onTouchEvent(MotionEvent event)

 3      {

 4          if(event.getAction()==MotionEvent.ACTION_DOWN){

 5              if(this.getCurrentFocus().getWindowToken()!=null){

 6                  //获取此Activity的软键盘服务

 7                  InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

 8                  //隐藏软件盘,下面两个方法选一个即可

 9                  imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

10                  imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);

11              }

12          }

13          return super.onTouchEvent(event);

14      }

 

 

你可能感兴趣的:(Android开发)