Hard keyboard Fail to focus editText

前端 未结 2 588
小蘑菇
小蘑菇 2020-12-31 19:30

I have a common EditText. It\'s very strange because I can\'t focus it when use hard keyboard. Context condition:

  1. switch Droid\'s hardkeyboard on
  2. star
相关标签:
2条回答
  • 2020-12-31 20:07

    As mentioned above this is clearly a bug with hard keyboard. If you have an EditText and a TabHost in your layout, on first key pressed, EditText lose focus and key press is sent to the activity instead. Here is a work around to this problem. Implement this in your activity.

    @Override
    
    public boolean onKeyDown(int keyCode, KeyEvent event){
    
        final EditText myInputField = (EditText) findViewById(R.id.MyInputEditText);
        // this will happen on first key pressed on hard-keyboard only. Once myInputField 
        // gets the focus again, it will automatically receive further key presses.
        if (!myInputField.hasFocus()){ 
            myInputField.requestFocus();
            myInputField.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }
    

    if you have multiple EditText fields, you will need to keep track of currently focused EditText in a class variable and use it in onKeyDown method.

    0 讨论(0)
  • 2020-12-31 20:23

    I have the same problem. I kinda agree with Jay. Typically TabHost, and/or TabActivity utilize a LocalActivityManager that keeps track of embedded Activities or appropriate ContentStrategy component that is displayed within the FrameLayout element. In simple words, this is a typical embedded Activities/ embedded Views layout problem. The Edit Text is on the top-most Activity/View that is taking the touch-screen space, while there's a core Activity that is actually hosting this Activity/View that probably is grabbing the InputMethodService focus and keeping it away from the Edit Text, only for the hard-keyboard scenario. The soft-keyboard just works fine.

    One change I did to my Edit Text is to change the InputType as purely decimal. So when the Edit Text gains focus, the soft keyboard shows a numeric key-pad and not the alphabetical qwerty key-pad. I ran it on a Motorla Droid Pro emulator, that I updated in Eclipse Plugins from the Motodev website. Apparently, when I try to enter text from the hard keyboard after having given focus for the Edit Text (and the soft-keyboard is showing a numeric key-pad), after I click 'ALT + 2', the soft-keyboard is reloaded as alphabetic key-pad while the Edit Text loses focus entirely.

    Seems like a serious bug to me in the Froyo release, insufficient support for hard-keyboard devices for edit text views in layouts (LinearLayout) that are embedded in other layouts (FrameLayout of a TabHost).

    0 讨论(0)
提交回复
热议问题