默认焦点和键盘到Android AlertDialog中的EditText

cai*_*ain 17 android dialog

我在Android中使用AlertDialog.Builder来快速提示用户输入文本.该对话框显示并且工作正常,但用户必须单击EditText字段才能加载软键盘.有没有办法打开键盘,并在我的对话框打开时给焦点?这是我的代码:

final Map<String,Object> rowData = itemList.get(mPosition);
                    final EditText input = new EditText(searchList.getContext());
                input.requestFocus();


                input.setSingleLine();
                final AlertDialog dialog = new AlertDialog.Builder(searchList.getContext())
                .setTitle(StringUtils.getSafeString(rowData.get("label")))
                .setView(input)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) { 
                        rowData.put("value", StringUtils.getSafeString(input.getText()));
                        searchList.invalidateViews();

                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }).create();
                dialog.show();
Run Code Online (Sandbox Code Playgroud)

Aha*_*med 28

使用以下代码.它对我有用.

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editText.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    editText.requestFocus();
Run Code Online (Sandbox Code Playgroud)


Rig*_*key 10

以编程方式将焦点设置在Android对话框中的EditText上时隐藏键盘.

我也有这个问题,这是一个非常简单的解决方案 - 这是我建议的解决方案.虽然它适用于我的DialogFragments,但我认为没有理由说它在你的情况下不起作用.

基本上,由于以编程方式创建视图,因此不会触发软键盘.实际修复只是将此行放在onCreateDialog方法中:

dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

DialogFragments上的Android文档:

如果用户专注于EditText,软键盘将自动出现.为了强制我们以程序化为重点,我们调用getDialog().getWindow().setSoftInputMode().请注意,您之前在Dialog中可能完成的许多Window操作仍然可以在DialogFragment中完成,但您必须调用getDialog().getWindow()而不是getWindow().

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    //setup your dialog builder and inflate the view you want here
    ...
    //Make sure your EditText has the focus when the dialog is displayed
    edit.requestFocus();
    //Create the dialog and save to a variable, so we can set the keyboard state
    Dialog dialog = builder.create();
    //now, set to show the keyboard automatically
    dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return dialog;
}
Run Code Online (Sandbox Code Playgroud)


May*_*ank 5

在您的 XML 布局中

称呼

<requestFocus/>
Run Code Online (Sandbox Code Playgroud)

在默认的 EditText 中

<EditText 
android:blabla
.... >
<requestFocus/>
</EditText>
Run Code Online (Sandbox Code Playgroud)