繁体   English   中英

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

[英]Default Focus and Keyboard to EditText in Android AlertDialog

我在 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();

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

    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();

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

我也有这个问题,这是一个非常简单的修复 - 这是我建议的解决方案。 尽管它对我来说适用于 DialogFragments,但我认为没有理由在您的情况下它不起作用。

基本上不会触发软键盘,因为视图是以编程方式创建的。 实际的修复只是将这一行放在 onCreateDialog 方法中:

dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

来自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;
}

在您的 XML 布局中

称呼

<requestFocus/>

在您的默认 EditText 中

<EditText 
android:blabla
.... >
<requestFocus/>
</EditText>

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 隐藏键盘使用:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(),0);

或尝试以下代码,但您必须设置 requestFocus() 或您的编辑文本

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

如果您需要更频繁地使用自定义视图

public class RequestFocusEditText extends AppCompatEditText {
    private RequestFocusEditText self;

    public RequestFocusEditText(final Context context, AttributeSet attrs) {
        super(context, attrs);
        this.self = this;

        setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                post(new Runnable() {
                    @Override
                    public void run() {
                    InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    requestFocus();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM