Android – EditText loses focus using the hard keyboard when tabhost exists in the activity

I display EditText in tabhost. Tabhost is located in sherlockfragmentactivity

Suppose I am in touch mode, I click EditText to focus on it and start typing. After typing 2-3 characters, I decide to use my hard keyboard. As long as I press the first hard key, I will exit touch mode, which will also move the focus back to the currently selected label. As long as I do not use the hard navigation key to restore the focus to my EditText, I will not be able to enter EditText

This problem can only be caused if tabhost is not in tabactivity

A quick example code to reproduce this problem may be the fragmenttabs activity of the – actionbarsherlock sample code. Select customtab and try typing any text in the search view in the action bar using the hard keyboard. The viewing focus just moves to the tab

The problem is also reproducible on Android 2.2, 4.0 and using ICs simulator

Does anyone have more information about this problem?

(there seems to be a solution mentioned here: https://stackoverflow.com/a/8684025/333137 But it looks like a hacker)

thank you.

Edit: after more investigation, I found the setup() function in the tabhost class. It needs to be called only when tabhost is not used in tabactivity. It registers a key listener and receives a callback only when a hard key is pressed

void setup(){
       // KeyListener to attach to all tabs. Detects non-navigation keys
        // and relays them to the tab content.
        mTabKeyListener = new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                    case KeyEvent.KEYCODE_DPAD_UP:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_ENTER:
                        return false;

                }
                **mTabContent.requestFocus(View.FOCUS_FORWARD);**
                return mTabContent.dispatchKeyEvent(event);
            }

I'm not sure why it calls requestfocus (view. Focus_forward), but it doesn't set the EditText in which the focus is set. In addition, if you search for a view in the action bar, it's completely outside the tab

resolvent:

Override tabhost as follows, which applies to me

public class TabHostExt extends TabHost {

    public TabHostExt(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TabHostExt(Context context) {
        super(context);
    }

    @Override
    public void onTouchModeChanged(boolean isInTouchMode) {
        // leave it empty here. It looks that when you use hard keyboard,
        // this method will be called and the focus will be token.
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>