简体   繁体   English

编辑框未在Android中获得焦点

[英]Edit box does not gain focus in android

i'm facing wired problem in android when i try to focus for a EditText control. 当我尝试着重于EditText控件时,我在android中面临有线问题。

In my application, i am dynamically adding the edit controls, whenever new control is added i wanted to give auto focus and popup the softkeypad automatically. 在我的应用程序中,我正在动态添加编辑控件,每当添加新控件时,我都希望自动对焦并自动弹出软键盘。

sometimes editbox get focus, but if i type text will not appear, sometimes text will appear in the other text control. 有时editbox会获得焦点,但是如果我键入文本将不会出现,则有时文本会出现在其他文本控件中。

i'm observing this behaviour in my phone. 我在手机中观察到这种行为。

Here is my code snippet which i used to provide focus. 这是我用来提供重点的代码段。

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

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

i tried setting focuschangelistener event, still it didnt worked :( 我尝试设置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);

            }
        }
    };

Kindly help me where is the problem.. thanks in advance 请帮助我哪里出了问题..在此先感谢

You are probably not on the UI thread when requesting focus which might cause the strange behavior of the EditText. 请求焦点时,您可能不在UI线程上,这可能会导致EditText的异常行为。 Try adding it to the message queue using the post(Runnable) method: 尝试使用post(Runnable)方法将其添加到消息队列中:

textView.post(new Runnable() {
    public void run() {
        textView.requestFocus();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM