简体   繁体   English

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

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

I am using the AlertDialog.Builder in Android to quickly prompt the user for text.我在 Android 中使用 AlertDialog.Builder 来快速提示用户输入文本。 The dialog shows up and works great, but the user must click on the EditText field to load the soft keyboard.该对话框显示并运行良好,但用户必须单击 EditText 字段以加载软键盘。 Is there any way to open the keyboard and give focus to the whenever my dialog is opened?有什么方法可以打开键盘并将焦点放在我的对话框打开时? Here is my code:这是我的代码:

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

Use the following code.使用以下代码。 It worked for me.它对我有用。

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

Hidden keyboard when programmatically setting focus on an EditText in an Android Dialog.以编程方式将焦点设置在 Android 对话框中的 EditText 上时隐藏键盘。

I had this problem as well and it was a pretty simple fix - here is my suggested solution.我也有这个问题,这是一个非常简单的修复 - 这是我建议的解决方案。 Although it worked on DialogFragments for me, I see no reason why it wouldn't work in your case.尽管它对我来说适用于 DialogFragments,但我认为没有理由在您的情况下它不起作用。

Basically the soft keyboard isn't triggered because the view is being created programmatically.基本上不会触发软键盘,因为视图是以编程方式创建的。 The actual fix was simply putting this line in the onCreateDialog method:实际的修复只是将这一行放在 onCreateDialog 方法中:

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

From the Android documentation on DialogFragments :来自DialogFragments上的 Android 文档:

If the user focuses on an EditText, the soft keyboard will automatically appear.如果用户专注于 EditText,软键盘会自动出现。 In order to force this to happen with our programmatic focus, we call getDialog().getWindow().setSoftInputMode().为了通过我们的编程重点强制执行此操作,我们调用 getDialog().getWindow().setSoftInputMode()。 Note that many Window operations you might have done previously in a Dialog can still be done in a DialogFragment, but you have to call getDialog().getWindow() instead of just getWindow().请注意,您之前可能在 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;
}

in your XML layout在您的 XML 布局中

call称呼

<requestFocus/>

inside your default EditText在您的默认 EditText 中

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

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

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

or try below code but you must set the requestFocus() or to your edittext或尝试以下代码,但您必须设置 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);
        }
    }
});

use a custom view if you need it more often如果您需要更频繁地使用自定义视图

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