安卓:对话框显示软键盘时自动对焦是一个EditText不工作 [英] Android: Dialog box show soft keyboard automatically when focus is on an EditText not working

查看:79
本文介绍了安卓:对话框显示软键盘时自动对焦是一个EditText不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext/2403977#2403977">http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext/2403977#2403977

我读过这篇文章,可以自动显示虚拟键盘的对话框显示时。然而,这不是为我工作。任何想法,为什么? eventhough编辑文本自动为焦点的时候DialogBox的出现,该事件不会触发。我也看到了onpostresume答案,但我不知道如何使用它。任何帮助是AP preciated。

I've read this post that automatically shows the virtual keyboard when a dialog box is shown. however, it's not working for me. any ideas why? eventhough the edit text is automatically on focus when the dialogbox appear, the event doesn't trigger. I've also read the onpostresume answer but I don't know how to apply it. any help is appreciated.

final Dialog dialog = new Dialog(ThesisI.this);
        dialog.setContentView(R.layout.budget_dialog);


        final EditText et = (EditText) dialog.findViewById(R.id.textComments);
        final Button enter = (Button) dialog.findViewById(R.id.buttonEnter);
        final Button cancel = (Button) dialog.findViewById(R.id.buttonCancel);

        enter.setOnClickListener(new View.OnClickListener() {
      @Override
   public void onClick(View v) {

      }
        });
        /**cancel */
        cancel.setOnClickListener(new View.OnClickListener() {
      @Override
   public void onClick(View v) {
   }
        });       
        dialog.show(); 

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

                }
            }
        });

不过,我注意到,如果我改变焦点的按钮,然后再次聚焦到编辑文本。本次活动的工作原理,使用下面这个code。

however, i noticed that if I change focus to the button then focus again to the edit text. this event works, using this code below.

et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                   InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                      inputMgr.toggleSoftInput(0, 0);
                }
            }
        });

在如何应用它的任何想法?

any idea on how to apply it?

推荐答案

你能做的就是尽量使用 postDelayed(可运行)的的EditText如下,

What you can do is try using postDelayed(Runnable) for EditText as below,

                ettext.requestFocus();
                ettext.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager keyboard = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                        keyboard.showSoftInput(ettext, 0);
                    }
                },200);

这篇关于安卓:对话框显示软键盘时自动对焦是一个EditText不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆