用户登录
用户注册

分享至

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

  • 作者: 给我一个吻s
  • 来源: 51数据库
  • 2023-02-09

问题描述

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

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.

软件
前端设计
程序设计
Java相关