使用硬件键盘时,Android TabHost选项卡会失去焦点 [英] Android TabHost tabs steal focus when using Hardware Keyboard

查看:221
本文介绍了使用硬件键盘时,Android TabHost选项卡会失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个TabHost,其中包含4个标签.在一些片段上,我们在布局中有许多EditText视图.

I currently have a TabHost containing 4 tabs. On a few of the fragments we have a number of EditText views within the layout.

我们注意到,当您尝试使用硬件键盘键入任何EditText视图时,焦点将从EditText窃取,并分配给TabHost中当前处于活动状态的选项卡.这仅在带有选项卡的屏幕上发生.有没有一种快速简单的方法来解决这个问题?

We have noticed that when you attempt to type into any of the EditText views using the hardware keyboard, the focus is stolen from the EditText and given to the currently active tab in the TabHost. This only occurs on screens with tabs. Is there a quick and simple way to solve this?

推荐答案

我在 http://code.google.com/p/android/issues/detail?id=2516 ,它比此处或错误报告页面上的任何解决方案都更有效,因为它可以解决根本原因而不是解决它.我让作者(g1adrift)解释:

I found this solution at http://code.google.com/p/android/issues/detail?id=2516 and it works better than any of the solutions here or on the bug report page, because it addresses the root cause instead of working around it. I'll let the author (g1adrift) explain:

在深入研究Android来源之后,我发现了该错误: TabHost在onAttachedToWindow()中注册一个OnTouchModeChangeListener 离开触摸模式时会失去焦点(也就是有人按下 键)(如果当前标签页内容视图没有焦点).虽然这 如果将整个版式设置为选项卡,或者只有一个 带有选项卡的布局部分会导致问题.

After digging extensively through the Android source, I found the bug: TabHost registers an OnTouchModeChangeListener in onAttachedToWindow() that steals focus when leaving touch mode (aka when someone presses a key) if the current tab content view doesn't have focus. While this may make sense if the whole layout is tabbed, if there is only a portion of the layout that has tabs, it causes issues.

此替代方法将删除该侦听器,因此使用该侦听器的所有工件 应该消失:

This workaround removes that listener, so all artifacts of using it should go away:

在onCreate()中,添加:

in onCreate(), add:

TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {

    @Override
    public void onViewDetachedFromWindow(View v) {}

    @Override
    public void onViewAttachedToWindow(View v) {
        mTabHost.getViewTreeObserver().removeOnTouchModeChangeListener(mTabHost);
    }
});

据说它仅适用于SDK 12+.作者还发布了针对早期SDK的解决方案.如果需要,请单击上面的链接,然后按"g1adrift"搜索帖子.

It supposedly only works for SDK 12+. The author also posted a solution for earlier SDKs. If you need it, click the link above and search for posts by "g1adrift".

这篇关于使用硬件键盘时,Android TabHost选项卡会失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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