使用硬键盘的时候TabHost是present在活动的EditText失去焦点 [英] EditText losing focus using hard keyboard when TabHost is present in activity

查看:364
本文介绍了使用硬键盘的时候TabHost是present在活动的EditText失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我展示里面tabhost一个EditText。 Tabhost是SherlockFragmentActivity里面。

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

假设我在触摸模式,我点击的EditText给它集中并开始打字就可以了。输入2-3个字符后,我决定用我的硬键盘。只要preSS第一硬键,我搬出了触控模式,这也使重点移回当前选定的标签。只要我不使用硬盘导航键使工作重心回到我的EditText,我将无法键入的EditText。

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.

此问题只时引起TabHost是不是里面TabActivity。

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

有一个快速的样品code重现此问题可能是 - 的ActionBarSherlock样品code FragmentTabs活动。选择CustomTab并尝试键入使用硬键盘操作栏上的搜索视图中的任何文字。见焦点只需移动到标签。

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.

但问题是重复性的在Android 2.2,4.0和使用ICS仿真器为好。

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

有没有人有更多的信息。关于这个问题的?

Does anyone have more info. about this issue?

(一种解决方法似乎是这里所说的: http://stackoverflow.com/a/8684025/333137 但它看起来像一个黑客)

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

感谢。

编辑:在更多的调查, 我发现这仅需要调用如果TabHost是不是里面TabActivity使用TabHost类的内部设置()函数。 它注册接收回调,只有当硬键是pressed键侦听器。

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);
            }

我不知道为什么它被调用requestFocus的(View.FOCUS_FORWARD),但是这并没有将焦点设置的EditText里面(它应该?)。另外,如果在操作栏中搜索视图中,这是完全外的标签。

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.

推荐答案

重写TabHost如下,它为我工作。

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

这篇关于使用硬键盘的时候TabHost是present在活动的EditText失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆