Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hard keyboard Fail to focus editText

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?

like image 803
Henry Sou Avatar asked May 15 '11 04:05

Henry Sou


1 Answers

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.

like image 196
Amir Sagri Avatar answered Oct 07 '22 19:10

Amir Sagri