1

当我尝试专注于 EditText 控件时,我在 android 中遇到了有线问题。

在我的应用程序中,我正在动态添加编辑控件,每当添加新控件时,我都想自动聚焦并自动弹出软键盘。

有时编辑框获得焦点,但如果我键入文本不会出现,有时文本会出现在其他文本控件中。

我正在手机中观察到这种行为。

这是我用来提供焦点的代码片段。

  if(mOldEditBox!=null)
            {
                mOldEditBox.clearFocus();
            }

            textView.requestFocus();
            //textView.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
            //textView.setOnFocusChangeListener(focuschange); 
            mOldEditBox = textView;

我尝试设置 focuschangelistener 事件,但它仍然没有工作:(

OnFocusChangeListener focuschange = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(hasFocus){

                EditText txt = (EditText)v;

                //txt.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad

                ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
        .showSoftInput(txt, InputMethodManager.SHOW_FORCED);

            }
        }
    };

请帮我解决问题..提前谢谢

4

1 回答 1

1

请求焦点时您可能不在 UI 线程上,这可能会导致 EditText 的奇怪行为。尝试使用以下方法将其添加到消息队列中post(Runnable)

textView.post(new Runnable() {
    public void run() {
        textView.requestFocus();
    }
});
于 2012-10-24T11:39:15.603 回答