«

Android alertDialog 动态添加edittext无法弹出键盘解决方案

时间:2024-3-2 18:48     作者:韩俊     分类: Android


严格来说这也不算是原创了,因为项目的原因需要在alertDialog根据spinner的选项动态生成控件,其中遇到一个恶心的问题,创建的edittext无法弹出键盘输入,找了很久,终于在stackoverflow里找到了方法。

问题的原因是这样的(我英文不好就不翻译了):

OK, so after reading a lot, I have figured out why this is a problem, and I do not need to use any workarounds.

The problem seems to be (at least in my case), that since the place where you enter text is hidden initially (or nested or something), AlertDialog is automatically setting the flag WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM (or some combination of that and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) so that things don't trigger a soft input to show up.

解决的方法就是加入这一句:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
或者在show()的时候这样弄:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

补充一点,这个好像是有版本要求的,具体大家去看stackoverflow的连接吧!

http://stackoverflow.com/questions/9102074/android-edittext-in-dialog-doesnt-pull-up-soft-keyboard

标签: android

热门推荐