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

时间:2012-03-21 20:23:03

标签: 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();

5 个答案:

答案 0 :(得分:23)

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

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

答案 1 :(得分:8)

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

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

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

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

来自DialogFragments上的Android文档:

  

如果用户专注于EditText,软键盘将会   自动出现。为了强迫这发生在我们身上   我们称之为程序化焦点   getDialog()。getWindow()。setSoftInputMode()。注意很多Window   您之前在Dialog中可能完成的操作仍然可以   在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;
}

答案 2 :(得分:1)

如果您经常需要,请使用自定义视图

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

答案 3 :(得分:0)

XML布局中的

致电

<requestFocus/>

在默认的EditText

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

答案 4 :(得分:0)

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

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