1

我有一些不推荐使用的代码需要修复。在对话框(showdialog)被关闭后,我想要一个edittext获得焦点,选择文本并启动键盘。这段代码不会发生这种情况。

使用此代码,没有焦点,但文本被选中。

...
alert.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    EditText txtE = (EditText) findViewById(R.id.ORE);
                    dismissDialog(DIALOG_NO_E);
                    txtE.selectAll();
                    txtE.requestFocus();                        
                }
            });
...

建议?

4

1 回答 1

1

但是,在 requestfocus 之后添加它可以使其正常工作。但感觉不对。

txtE.postDelayed(new Runnable() {
                        public void run() {
                            android.view.inputmethod.InputMethodManager keyboard = (android.view.inputmethod.InputMethodManager) getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
                            keyboard.showSoftInput(txtE, 0);
                        }
                    }, 200);

最终结果

...
alert.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dismissDialog(DIALOG_NO_E);
                    txtE.selectAll();
                    txtE.requestFocus();                        
                }
            });
txtE.postDelayed(new Runnable() {
                            public void run() {
                                android.view.inputmethod.InputMethodManager keyboard = (android.view.inputmethod.InputMethodManager) getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
                                keyboard.showSoftInput(txtE, 0);
                            }
                        }, 200);
...

如果有人有更好的解决方案或为什么我必须这样做的答案,我会在星期一给你答案的分数。

为此,我必须将其创建txtE为在构造函数中初始化的静态变量。那在我的灵魂里一点都感觉不好。因此,如果有人有更好的想法可行,请告诉。

于 2013-07-19T13:20:15.010 回答