显示软键盘时AlertDialog.Builder对象是打开 [英] Displaying soft keyboard whenever AlertDialog.Builder object is opened
问题描述
我的$ C $下打开一个输入对话框的内容如下:
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.
以下给出的建议<一href="http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext">here我曾尝试插入:
Following the advice given here I have tried inserting:
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 code适用于一个AlertDialog对象,但不是AlertDialog.Builder。我应该如何修改我的code键使软键盘automatcially出现。
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.
推荐答案
随着穆尔Votema的鼓励下(看他的回答以上)我已经通过构建基于对话框类的自定义对话框回答我的问题。与基于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.
For guidance on building a custom dialog I found this web page and this especially helpful.
这篇关于显示软键盘时AlertDialog.Builder对象是打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!