android - EditText 未收到 TAB 键事件 - stock soft vk

标签 android listview tabs android-edittext keyevent

我的应用有一个 ListView 和一个位于其下方的 EditText。出于某种原因,TAB 键不会触发 onKeyListener。我正在处理的所有其他键(DEL、ENTER、DPAD_UP/DOWN/CENTER)都可以正常接收。我在 dispatchKeyEvent 中添加了一个断点,同样没有收到 TAB 事件。

我的应用程序之前有一个用于显示文本的大型 TextView,在此期间,可以正常接收 TAB 事件。 ListView 现在已经取代了 TextView

我完全不明白为什么不再接收 TAB 事件。这是在股票 Xoom 上,运行 ICS 4.0.4 和股票 N1,2.3.6。

我已经将我当前的代码与使用 TextView 的版本进行了比较,大部分代码只是为了处理 ListView 而不是 TextView 。除了 nextFocusLeftnextFocusRight 属性外,EditText 没有其他任何更改。

编辑:我刚刚尝试使用 Go Keyboard 和 Hacker's Keyboard,TAB 接收正常。看起来这只是一些虚拟键盘

最佳答案

我想我可能看到了问题。查看 ListView.java 的源代码,有一种机制可以使用在列表项内移动焦点的关键事件。查看此方法之前的注释以及方法中间的注释 block 。

/**
 * To avoid horizontal focus searches changing the selected item, we
 * manually focus search within the selected item (as applicable), and
 * prevent focus from jumping to something within another item.
 * @param direction one of {View.FOCUS_LEFT, View.FOCUS_RIGHT}
 * @return Whether this consumes the key event.
 */
private boolean handleHorizontalFocusWithinListItem(int direction) {
    if (direction != View.FOCUS_LEFT && direction != View.FOCUS_RIGHT)  {
        throw new IllegalArgumentException("direction must be one of"
                + " {View.FOCUS_LEFT, View.FOCUS_RIGHT}");
    }

    final int numChildren = getChildCount();
    if (mItemsCanFocus && numChildren > 0 && mSelectedPosition != INVALID_POSITION) {
        final View selectedView = getSelectedView();
        if (selectedView != null && selectedView.hasFocus() &&
                selectedView instanceof ViewGroup) {

            final View currentFocus = selectedView.findFocus();
            final View nextFocus = FocusFinder.getInstance().findNextFocus(
                    (ViewGroup) selectedView, currentFocus, direction);
            if (nextFocus != null) {
                // do the math to get interesting rect in next focus' coordinates
                currentFocus.getFocusedRect(mTempRect);
                offsetDescendantRectToMyCoords(currentFocus, mTempRect);
                offsetRectIntoDescendantCoords(nextFocus, mTempRect);
                if (nextFocus.requestFocus(direction, mTempRect)) {
                    return true;
                }
            }
            // we are blocking the key from being handled (by returning true)
            // if the global result is going to be some other view within this
            // list.  this is to acheive the overall goal of having
            // horizontal d-pad navigation remain in the current item.
            final View globalNextFocus = FocusFinder.getInstance().findNextFocus(
                    (ViewGroup) getRootView(), currentFocus, direction);
            if (globalNextFocus != null) {
                return isViewAncestorOf(globalNextFocus, this);
            }
        }
    }
    return false;
}

单个列表元素中是否有多个可聚焦的项目?如果是这样,此代码将使用 tab 键。如果是这种情况,那么您可能希望使某些项目无法聚焦或考虑其他设计选项。

关于android - EditText 未收到 TAB 键事件 - stock soft vk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467184/

相关文章:

android - 如何使用以下 JSON 响应在另一个 recyclerView 中显示一个 recyclerView

JavaScript 文件不适用于 HTML

jquery - 如何阻止 jQuery 标签 CSS 被覆盖

单独进程中服务中的 android.os.NetworkOnMainThreadException

android - 从 Assets 文件夹错误读取sqlite

android - Android 设备上的 60/120fps 预览数据?

c# - 当 ListView 中的数据更改时,WPF 获取 "old"值

android - 在 GridView 中从 SD 卡目录加载图像

android - Recyclerview 列表结束覆盖颜色和 alpha

android 禁用 move 到下一个字段