android点击屏幕隐藏小键盘

原文: android点击屏幕隐藏小键盘

fragment 下隐藏点击空白处隐藏小键盘  

view.setOnTouchListener(new OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    InputMethodManager manager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    if(event.getAction() == MotionEvent.ACTION_DOWN){
      if(getActivity().getCurrentFocus()!=null && getActivity().getCurrentFocus().getWindowToken()!=null){
        manager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
      }
    }
    return false;
  }
});

activity 下隐藏点击空白处隐藏小键盘

@Override
public boolean onTouchEvent(MotionEvent event) {
  InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  if(event.getAction() == MotionEvent.ACTION_DOWN){
    if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
      manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
  }
  return super.onTouchEvent(event);
}

 

显示小键盘

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

你可能感兴趣的:(android点击屏幕隐藏小键盘)