Android TimePicker(轮式)无法正确响应 ScrollView 内的轻拂手势

发布于 2024-12-26 07:40:49 字数 151 浏览 1 评论 0原文

我有一个包含滚动视图的对话框,其中包含带有两个时间选择器的布局。

时间选择器是 ICS 中较新风格的时间选择器。

问题是,当您通过拖动或轻弹滚轮来更改时间时,它们似乎会争夺焦点。它将稍微改变时间,然后布局将滚动。

有什么想法吗? 提前致谢。

I have a dialog box that contains a Scrollview, which contains a layout with two TimePickers.

The timepickers are the newer style ones, what's in ICS.

The problem is that they seem to fight for focus when you change the time by dragging the wheel, or flicking it. It will change the time just a little, and then the layout will scroll instead.

Any ideas?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2025-01-02 07:40:49

我在使用 Holo 主题时遇到了同样的问题,在这里我找到了解决方案: https://groups.google.com/forum/?fromgroups#!topic/android-developers/FkSfJI6dH8w

您必须实现自定义 DatePicker 或TimePicker 并重写以下方法:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
    {
        ViewParent p = getParent();
        if (p != null)
            p.requestDisallowInterceptTouchEvent(true);
    }

    return false;
}

I had the same problem when using the Holo theme, and here is where I found the solution: https://groups.google.com/forum/?fromgroups#!topic/android-developers/FkSfJI6dH8w

You must implement your custom DatePicker or TimePicker and override the following method:

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
    {
        ViewParent p = getParent();
        if (p != null)
            p.requestDisallowInterceptTouchEvent(true);
    }

    return false;
}
少女的英雄梦 2025-01-02 07:40:49

由于 Klemens Zleptnig 的链接已损坏,因此这里是一个完整的示例。顺便说一句,此修复也有助于 TabLayout 的滚动。我排除了 TimePicker 顶部大数字周围的区域,因为它们无论如何都不需要滚动事件。

xml:

<com.name.app.MyTimePicker
                android:id="@+id/timePicker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
.../>

java:

public class MyTimePicker extends TimePicker {
    public MyTimePicker(Context context) {
        super(context);
    }

    //This is the important constructor
    public MyTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        //Excluding the top of the view
            if(ev.getY() < getHeight()/3.3F)
                return false;

            ViewParent p = getParent();
            if (p != null)
                p.requestDisallowInterceptTouchEvent(true);
        }

        return false;
    }
}

Because the link from Klemens Zleptnig is broken, here is a complete example. This fix helps with the scroll of a TabLayout too btw. I excluded the area around the big numbers in the top of the TimePicker because they don't need the scroll event anyway.

xml:

<com.name.app.MyTimePicker
                android:id="@+id/timePicker"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
.../>

java:

public class MyTimePicker extends TimePicker {
    public MyTimePicker(Context context) {
        super(context);
    }

    //This is the important constructor
    public MyTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyTimePicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        //Excluding the top of the view
            if(ev.getY() < getHeight()/3.3F)
                return false;

            ViewParent p = getParent();
            if (p != null)
                p.requestDisallowInterceptTouchEvent(true);
        }

        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文