8

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. start the activity
  3. click the editText to input
  4. Fail to input. When you press any key, the editText lost focus.

To get focus: press Dpad and you will see the focus starts from the 1st widget in the screen. And finally focus on the target EditText. Then you can input. Without this, you can't input with hard keyboard at all.

Soft keyboard doesn't have such focus problem.

I am using android 2.2. Is this a system bug?

Matt Briançon
  • 1,064
  • 16
  • 24
Henry Sou
  • 882
  • 2
  • 12
  • 19
  • I'm experiencing the same issue with an EditText widget that I'm using as a password field. Other EditText widgets (both password and non-password) work just fine in other layouts. If it matters, the broken widget is part of a LinearLayout styled with ButtonBar (though I think that just adds some padding and a background). – Matt Briançon Jul 10 '11 at 04:02
  • I guess the EditText may be wrapped by something else and then it's broken. A similar condition is wrap a list view in a scrollbar. I try to use a single edit text and it's OK. I will check the layout with the information you provide and comment back later. – Henry Sou Jul 11 '11 at 08:52
  • I still can't find any solution for it. All I can find is: Where the EditText fails to gain hard keyboard focus there is a TabHost as the top. Any idea about it? – Henry Sou Jul 15 '11 at 08:49
  • Hmmm, mine doesn't have a TabHost. I'll try to see if it's reported as a bug yet (and start one if not). – Matt Briançon Jul 15 '11 at 13:18
  • I'm having a similar problem. I have a form in a LinearLayout within in a FrameLayout. The FrameLayout also has a TabHost, but even if I hide the tab host (visibility = none) same problem. Seems to be some sort of issue with the combination of views? Happening in emulator on 1.6 and 2.1 – Jay Oct 26 '11 at 02:03

2 Answers2

9

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.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Amir Sagri
  • 91
  • 1
  • 2
0

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).

AndroidRocks
  • 292
  • 4
  • 16