每当打开AlertDialog.Builder对象时显示软键盘

pre*_*bgg 32 keyboard android android-edittext android-alertdialog

我打开输入对话框的代码如下:

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);
Run Code Online (Sandbox Code Playgroud)

这很好用,除了我必须在软键盘出现之前点击文本输入行.

按照这里给出的建议我尝试插入:

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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

但Eclipse对象"没有为AlertDialog.Builder类型定义方法getWindow()".

似乎setOnFocusChangeListener代码适用于AlertDialog对象,但不适用于AlertDialog.Builder.我应该如何修改我的代码以使软键盘自动显示.

Def*_*ged 78

只要您始终需要在对话框打开后立即显示键盘而不是一次特定的窗体小部件内部获得焦点(例如,如果对话框只显示EditText一个按钮),您可以执行以下操作:

AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();
Run Code Online (Sandbox Code Playgroud)

而不是.show()立即调用您的构建器,而是可以调用.create()哪个允许您在将其显示到屏幕上之前对其进行一些额外的处理.

  • 最简单的答案,对我来说非常有效. (3认同)
  • 真正的问题是当你在alert show()之前调用setSoftInputMode()时它的布局不同(很奇怪......) - 我的自定义视图与Ok/Cancel按钮重叠.所以CALL setSoftInputMode()AFTER show()!!! (2认同)

pre*_*bgg 15

这是对miannelle的回应.

选择菜单选项时,将调用以下方法:

private void addNote() {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.textentryalertdialog);
    dialog.setTitle("Add note");
    TextView msgText = (TextView) dialog.findViewById(R.id.messagetext);
    msgText.setText("Whatever prompt you want");
    final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext);
    Button okButton = (Button) dialog.findViewById(R.id.OKButton);
    okButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            dialog.dismiss();
            // app specific code
        }           
    });
    Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton);
    cancelButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            dialog.dismiss();
        }           
    });
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    dialog.show();
}
Run Code Online (Sandbox Code Playgroud)

textentryalertdialog.xml文件定义包含的线性布局

TextView android:id ="@ + id/messagetext"......

EditText android:id ="@ + id/my_edittext"......

按钮android:id ="@ + id/OKButton"......

按钮android:id ="@ + id/CancelButton"...

我希望这有帮助.


pre*_*bgg 6

在Mur Votema的鼓励下(见上面的回答)我通过构建基于Dialog类的自定义对话框来回答我的问题.与基于AlertDialog.Builder的警报不同,这样的自定义对话框确实接受getWindow().setSoftInputMode(...)命令,因此允许自动显示软键盘.

有关构建自定义对话框的指导,我找到了此网页,尤其有用.