首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打开AlertDialog.Builder对象时显示软键盘

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

Stack Overflow用户
提问于 2010-10-30 02:12:14
回答 14查看 31.9K关注 0票数 35

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

代码语言:javascript
复制
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);

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

按照给出的建议,我尝试插入here

代码语言:javascript
复制
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认为“没有为AlertDialog.Builder类型定义getWindow()方法”。

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

EN

Stack Overflow用户

发布于 2011-05-25 19:44:17

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

代码语言:javascript
复制
AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();

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

票数 83
EN
查看全部 14 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4054662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档