相信很多人都做过自定义的alterdialog,但不知道有没有发现当alterdialog布局中有exittext的时候即使edittext获得焦点也不能弹出软键盘,是不是很操蛋,而将alterdialog换成dialog的时候就可以的弹出了。下面就去分析一下为什么会这样
看一下alterdialog这个类,点击去就会看到头部注释上有这么一段话
The AlertDialog class takes care of automatically setting
* {@link #FLAG_ALT_FOCUSABLE_IM
* .FLAG_ALT_FOCUSABLE_IM} for you based on whether
* any views in the dialog return true from {@link View#onCheckIsTextEditor()
* ()}. Generally you want this set for a Dialog
* without text editors, so that it will be placed on top of the current
* input method UI. You can modify this behavior by forcing the flag to your
* desired mode after calling {@link #onCreate}.
什么意思呢,大概的意思就是当alterdialog检测到布局中有view调用onCheckIsTextEditor返回true的时候会自动给窗口设置上#FLAG_ALT_FOCUSABLE_IM这个flag,那么onCheckIsTextEditor是什么意思?看下解释
Check whether the called view is a text editor, in which case it
* would make sense to automatically display a soft input window for
* it. Subclasses should override this if they implement
* {@link #onCreateInputConnection(EditorInfo)} to return true if
* a call on that method would return a non-null InputConnection, and
* they are really a first-class editor that the user would normally
* start typing on when the go into a window containing your view.
啥意思呢,大概也就是说检测是否是文本编辑框,若是的话就返回true,好了那么就知道了当我们的alterdialog布局中有文本编辑框的时候会自动给窗口设置上#FLAG_ALT_FOCUSABLE_IM,那么问题来了这个flag是什么意思,还是看解释吧
Window flag: invert the state of {@link #FLAG_NOT_FOCUSABLE} with
* respect to how this window interacts with the current method. That
* is, if FLAG_NOT_FOCUSABLE is set and this flag is set, then the
* window will behave as if it needs to interact with the input method
* and thus be placed behind/away from it; if FLAG_NOT_FOCUSABLE is
* not set and this flag is set, then the window will behave as if it
* doesn't need to interact with the input method and can be placed
* to use more space and cover the input method.
意思好像大概也许差不多就是设置了这个flag就不能在弹出软键盘
那知道了原因怎么解决呢,当然就是把这个flag移除就好了,
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
如果你还想弹出dialog的时候自动弹出软键盘那再加上
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
好了就这样吧,搬砖去了