ScrollView中禁止EditText焦点自动切换

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;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容