每当打开 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 对象时显示软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)