软键盘未显示在 AlertDialog 中
所以我有一个菜单项显示 AlertDialog
,其中包含 EditText
,问题是,尽管它处于焦点状态,但软键盘不会显示,直到我单击编辑文本,任何人都得到一个解决方案?我尝试过,
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
但它不起作用。这是我的代码
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return (applyMenuChoice(item) || super.onOptionsItemSelected(item));
}
private boolean applyMenuChoice(MenuItem item) {
switch (item.getItemId()) {
case SEARCH:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setMinimumWidth(300);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString().trim();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
});
alert.show();
return (true);
case DELETE:
getListView().setAdapter(null);
return (true);
}
return (false);
}
So i have a menu item that shows AlertDialog
with a EditText
in it, the problem is that although it is focused the softkeyboard doesn show until I click on the edittext, anyone got a solution ? I tried
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
but it doesn work. Here is my code
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return (applyMenuChoice(item) || super.onOptionsItemSelected(item));
}
private boolean applyMenuChoice(MenuItem item) {
switch (item.getItemId()) {
case SEARCH:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setMinimumWidth(300);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString().trim();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
});
alert.show();
return (true);
case DELETE:
getListView().setAdapter(null);
return (true);
}
return (false);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个代码,
打开
Try this code,
TO OPEN
下面的解决方案对我有用
只需评论
alert.show();
在您的代码中并嵌入以下代码
硬编码延迟不建议使用,因为它们可能会在不同条件/不同设备下引入不可预测的行为。
The below solution works for me
Just comment the
alert.show();
in your code and embed the below code
Hard-coded delays are never recommended because they may introduce unpredictable behavior under different conditions / different devices.