【问题标题】:EditText not receiving TAB key events - stock soft vkEditText 没有收到 TAB 键事件 - 股票软 vk
【发布时间】:2012-05-15 01:57:26
【问题描述】:

我的应用程序下方有一个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 接收良好。看来这只是一些虚拟键盘

【问题讨论】:

    标签: android listview tabs android-edittext keyevent


    【解决方案1】:

    我想我可能会看到问题所在。查看 ListView.java 的源代码,有一种机制可以使用在列表项中转移焦点的关键事件。查看该方法前面的 cmets 以及该方法中间的 cmets 块。

    /**
     * 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 键。如果是这种情况,那么您可能希望使某些项目无法聚焦或考虑其他设计选项。

    【讨论】:

    • 我设置了一个测试项目来测试这个,不幸的是它没有工作。 edittext 总是有焦点,所以它应该总是接收 keyevent,或者至少,它应该被 dispatchKeyEvent 捕获。这适用于黑客键盘,但不适用于普通键盘。我已经在这里上传了测试项目dl.dropbox.com/u/78755665/Test.zip 列表视图中的所有内容都设置为不可聚焦
    • 当我在三星 Galaxy SII Epic 4G 上运行它时,软键盘甚至没有显示 Tab 键。
    • 模拟器键盘也没有tab键。
    • 建议,尝试使用 .setKeyListener() 代替 setOnKeyListener() 并监听按键。
    • setKeyListener 不适合这个,如果文档有什么要参考的话
    猜你喜欢
    • 2012-11-08
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    相关资源
    最近更新 更多