Displaying soft keyboard whenever AlertDialog.Builder object is opened(每当打开 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 对象时显示软键盘
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01