AlertDialog与EditText,自动打开软键盘与焦点上EditText不起作用

我试图得到一段代码工作,它应该集中一个AlertDialog的EditText一旦显示,然后自动打开软键盘。 相反,它只是让屏幕变暗。

Builder builder = new Builder(this); final EditText input = new EditText(this); AlertDialog dialog = builder.create(); builder .setTitle(R.string.dialog_title_addsubject) .setMessage(R.string.dialog_addsubject) .setView(input) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String value = input.getText().toString(); if (input.getText().toString().trim().length() == 0) { Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show(); } else { db.insertSubject(value); getData(); } } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); input.requestFocus(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); dialog.show(); 

我尝试了很多方法来做这件事,但都没有成功。 我希望你们能帮助我。 提前致谢!

Solutions Collecting From Web of "AlertDialog与EditText,自动打开软键盘与焦点上EditText不起作用"

好吧,我设法让它工作:

 Builder builder = new Builder(this); final EditText input = new EditText(this); builder .setTitle(R.string.dialog_title_addsubject) .setMessage(R.string.dialog_addsubject) .setView(input) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String value = input.getText().toString(); if (input.getText().toString().trim().length() == 0) { Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show(); } else { db.insertSubject(value); getData(); } InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(input.getWindowToken(), 0); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(input.getWindowToken(), 0); } }); builder.show(); input.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

这个方法不需要对话框,所以我可以使用builder.show()来显示对话框,Saber提供的代码打开软键盘。 每个button中的另一个代码片段会自动closures软键盘。

你可以使用它来代替dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

dialog.show();之后调用它dialog.show();

  public void selectContact(Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.mipmap.icon); builder.setTitle(R.string.title); builder.setPositiveButton(android.R.string.ok, context); builder.setNegativeButton(android.R.string.cancel,context); builder.setView(View.inflate(context, R.layout.dialog, null)); AlertDialog alertDialog = builder.create(); alertDialog.setOnShowListener(this); //Add listener alertDialog.show(); } 

在onShow中打开keyborad: –

  @Override public void onShow(DialogInterface dialog) { EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.number); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); } 

尝试之后显示一下 –

 new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { input.requestFocus(); dialog.getWindow(). setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); dialog.show(); }, 1000)