AlertDialog与EditText上

AlertDialog与EditText上

本文介绍了AlertDialog与EditText上,打开软键盘,重点是自动的EditText不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分一杯羹的code的工作应尽快在AlertDialog关注一个EditText,因为它显示,然后自动打开软键盘。相反,它只是使屏幕变为暗。

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!

推荐答案

好吧,我设法得到它的工作:

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

此方法不需要一个对话框,这样我就可以用builder.show()显示对话框,然后按佩剑提供的code打开软键盘。在每个按钮的另一个code段自动关闭软键盘。

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.

这篇关于AlertDialog与EditText上,打开软键盘,重点是自动的EditText不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:11