问题描述
我打开输入对话框的代码如下:
my code for opening an input dialog reads as follows:
final alertdialog.builder alert = new alertdialog.builder(this); alert.settitle("dialog title"); alert.setmessage("request information"); layoutinflater factory = layoutinflater.from(this); final view textentryview = factory.inflate(r.layout.edittextautotextlayout, null); final edittext inputbox = (edittext) textentryview.findviewbyid(r.id.my_et_layout); alert.setview(inputbox);
这很好用,只是我必须在软键盘出现之前点击文本输入行.
this works fine except that i have to tap the text entry line before the soft keyboard appears.
按照这里给出的建议 我试过插入:
inputbox.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (hasfocus) { alert.getwindow().setsoftinputmode( windowmanager.layoutparams.soft_input_state_always_visible); } } });
但是 eclipse 对象方法 getwindow() 没有为 alertdialog.builder 类型定义".
but eclipse objects that "the method getwindow() is not defined for the type alertdialog.builder".
似乎 setonfocuschangelistener 代码适用于 alertdialog 对象,但不适用于 alertdialog.builder.我应该如何修改我的代码以使软键盘自动出现.
it seems that the setonfocuschangelistener code works for an alertdialog object but not an alertdialog.builder. how should i modify my code to make the soft keyboard appear automatcially.
推荐答案
在 mur votema 的鼓励下(见上面他的回答),我通过构建一个基于 dialog 类的自定义对话框来回答我的问题.与基于 alertdialog.builder 的警报不同,这种自定义对话框确实接受 getwindow().setsoftinputmode(...) 命令,因此允许自动显示软键盘.
with the encouragement of mur votema (see his answer above) i have answered my question by building a custom dialog based on the dialog class. unlike an alert based on alertdialog.builder such a custom dialog does accept the getwindow().setsoftinputmode(...) command and therefore allows the soft keyboard to be displayed automatically.
有关构建自定义对话框的指导,我发现 this网页和这个特别有用.
for guidance on building a custom dialog i found this web page and this especially helpful.