美文网首页
ScrollView中禁止EditText焦点自动切换

ScrollView中禁止EditText焦点自动切换

作者: xyf_tc | 来源:发表于2017-03-09 09:27 被阅读0次

ScrollView中如果多个EditText,在某个EditText失去焦点时,会自动切换焦点到下一个EditText。

如下为ScrollView的的焦点分发函数

    /**
     * When looking for focus in children of a scroll view, need to be a little
     * more careful not to give focus to something that is scrolled off screen.
     * 
     * This is more expensive than the default {@link android.view.ViewGroup}
     * implementation, otherwise this behavior might have been made the default.
     */
    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
    {

        // convert from forward / backward notation to up / down / left / right
        // (ugh).
        if (direction == View.FOCUS_FORWARD)
        {
            direction = View.FOCUS_DOWN;
        }
        else if (direction == View.FOCUS_BACKWARD)
        {
            direction = View.FOCUS_UP;
        }

        final View nextFocus = previouslyFocusedRect == null ? FocusFinder.getInstance().findNextFocus(this, null, direction) : FocusFinder.getInstance()
                .findNextFocusFromRect(this, previouslyFocusedRect, direction);

        if (nextFocus == null)
        {
            return false;
        }

        if (isOffScreen(nextFocus))
        {
            return false;
        }

        return nextFocus.requestFocus(direction, previouslyFocusedRect);
    }

如果不需要该效果,继承ScrollView并重写该方法即可,取消焦点时就不会自动分发

    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
        return true;
    }

相关文章

网友评论

      本文标题:ScrollView中禁止EditText焦点自动切换

      本文链接:https://www.haomeiwen.com/subject/zvqwgttx.html