EditText losing focus using hard keyboard when TabHost is present in activity

纵饮孤独 提交于 2020-02-20 05:55:33

问题


I am displaying an EditText inside tabhost. Tabhost is inside a SherlockFragmentActivity.

Suppose I am in touch mode and I click on EditText to give it focus and start typing on it. After typing 2-3 chars, I decided to use my hard keyboard. As soon as press first hard key, I move out of touch mode and this also causes the focus to moves back to the currently selected tab. As long as I do not use hard navigation keys to bring back the focus to my EditText, I will not be able to type into EditText.

This issue is caused only when TabHost is not inside TabActivity.

A quick sample code to reproduce this problem could be - FragmentTabs activity of ActionBarSherlock sample code. Select CustomTab and try to type any text inside the search view in action bar using the hard keyboard. See focus simply moves to tab.

Problem is reproducible on android 2.2, 4.0 and using ICS emulator as well.

Does anyone have more info. about this issue?

(A workaround seems to be mentioned here: https://stackoverflow.com/a/8684025/333137 but it looks like a hack)

Thanks.

Edit: Upon more investigation, I found setup() function inside TabHost class which needs to be called only if TabHost is not used inside TabActivity. It registers a key listener which receives callback only when 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 am not sure why it is calling requestFocus(View.FOCUS_FORWARD) but this does not set the focus to EditText inside it (which it should?). Also, in case of search view in action bar, it is totally outside tab.


回答1:


Override the TabHost as below, it works for 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.
    }
}


来源:https://stackoverflow.com/questions/11277491/edittext-losing-focus-using-hard-keyboard-when-tabhost-is-present-in-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!