android - 在 AlertDialog 上自动打开软键盘
问题描述
我正在创建一个AlertDialog.Builder
,我正在分配一个EditText
来获取输入。当它打开时,键盘不会自动显示。这是我的代码:
private void AskForInput(int inputType, String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
final EditText input = new EditText(this);
input.setInputType(inputType);
builder.setView(input);
builder.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
keyNumberValue = input.getText().toString() + "#";
//TODO: do things...
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
我尝试使用从另一个帖子链接中获得的建议来解决:
AlertDialog alertToShow = builder.create();
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();
但我仍然必须点击EditText
打开键盘。任何想法?
解决方案
推荐阅读
- c - 是什么导致此函数中的分段错误?
- android - 如何将插图应用于带有图像的 CollapsingToolbarLayout,因此工具栏中的图像绘制在状态栏后面?
- javascript - 条件为真时打开模式 - Javascript 和引导程序
- javascript - 使用 React Navigation 隐藏基于 AsyncStorage 项的底部选项卡项
- javascript - 如何根据其他两个较早的下拉选项动态调整下拉选项?
- c++ - shared_ptr 类向量的向量初始化问题
- python - 如何确定 PyQt5 对话框是否会在屏幕外显示
- c# - DirectoryInfo 的 GetFiles 未显示目录中的实际内容
- bash - bash shell 附加到变量的挫败感
- c# - 如何在使用 Host.CreateDefaultBuilder 安装的 Web 主机中配置日志记录