关于Android中EditText自动获取焦点并弹出键盘的相关设置

在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法:

  需求:EditText自动获取焦点并弹出键盘,代码:

      EditText.setFocusable(true);
      EditText.setFocusableInTouchMode(true);
      EditText.requestFocus();
  需求:EditText不会自动获取焦点并且不会弹出键盘,代码:
      将其父控件设置:
      Parent.setFocusable(true);
      Parent.setFocusableInTouchMode(true);

//打开软键盘
InputMethodManager imm = (InputMethodManager)MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);    //InputMethodManager.SHOW_FORCED

//关闭软键盘
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

仅分享给大家,希望能够帮到大家。





原文地址:https://www.cnblogs.com/jeffen/p/6839607.html