我打开输入对话框的代码如下:
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:
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。我应该如何修改我的代码,使软键盘自动显示。
发布于 2011-05-25 19:44:17
只要您总是需要在对话框打开后立即显示键盘,而不是在内部的特定表单小部件获得焦点时(例如,如果对话框只显示一个EditText和一个按钮),您就可以执行以下操作:
AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();您可以调用.create(),而不是立即调用构建器上的.show(),它允许您在将其显示在屏幕上之前对其进行一些额外的处理。
https://stackoverflow.com/questions/4054662
复制相似问题