Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

android - AlertDialog with EditText, open soft keyboard automatically with focus on EditText doesn't work

I'm trying to get a piece of code work which should focus an EditText in an AlertDialog as soon as it shows and then automatically open the soft keyboard. Instead, it just makes the screen go darker.

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

I tried many ways of doing this but none worked. I hope you guys can help me here. Thanks in advance!

question from:https://stackoverflow.com/questions/12997273/alertdialog-with-edittext-open-soft-keyboard-automatically-with-focus-on-editte

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok I managed to get it working:

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

This method doesn't need a dialog so I can use builder.show() to show the dialog and the code provided by Sabre opens the soft keyboard. Another code snippet in each of the buttons closes the soft keyboard automatically.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...