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

Displaying soft keyboard whenever AlertDialog.Builder object is opened(每当打开 AlertDialog.Builder 对象时显示软键盘)

本文介绍了每当打开 AlertDialog.Builder 对象时显示软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于每当打开 AlertDialog.Builder 对象时显示软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:每当打开 AlertDialog.Builder 对象时显示软键盘

基础教程推荐