用于解决AlertDialog中需要向EditText输入内容却不能弹出输入法

很多时候需要以AlertDialog对话框的形式和用户进行交互,因为对于一些小数据数据信息交互,而且可以不必要的打开新的Activity而累赘。

但当AlerDialog里面有EditText的时候需要输入内容,而且是用这种方式设置布局的话
dialog.getWindow().setContentView(layout)
就会出现弹不起软键盘的尴尬。原来,通过阅读官方文档:
其中有一段:

Note: Activities provide a facility to manage the creation, saving and restoring of dialogs. See onCreateDialog(int)onPrepareDialog(int, Dialog)showDialog(int), anddismissDialog(int). If these methods are used, getOwnerActivity() will return the Activity that managed this dialog.

Often you will want to have a Dialog display on top of the current input method, because there is no reason for it to accept text. You can do this by setting theWindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM window flag (assuming your Dialog takes input focus, as it the default) with the following code:

 getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
         WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
这是默认情况下隐藏软键盘的方法,要重新显示软键盘,要执行下面这段代码即可:

alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);


AlertDialog.setView()则不会出现以上问题。

另外:为了防止弹出输入法时 把后面的背景挤变形,可以在Manifest里添加:

android:windowSoftInputMode="adjustPan|stateHidden"




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值