简体   繁体   English

android - 滚动输入 - 滚动视图内的日期选择器

[英]android - scroll input - date picker inside scroll view

I have the following layout for a dialog (I need to get user's birthday information) :我有以下对话框布局(我需要获取用户的生日信息):

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Where were you born ?"
    android:id="@+id/textView_BirthPlace"
    android:textStyle="bold"
    android:textSize="13dp"
    android:textColor="@color/heading_text_color"
    android:layout_margin="20dp"/>

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="What time were you born ?"
    android:id="@+id/textView_BirthTime"
    android:textStyle="bold"
    android:textSize="13dp"
    android:textColor="@color/heading_text_color"
    android:layout_margin="20dp"/>

<TimePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timePicker"
    android:layout_marginLeft="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="When were you born ?"
    android:id="@+id/textView_BirthDate"
    android:textStyle="bold"
    android:textSize="13dp"
    android:textColor="@color/heading_text_color"
    android:layout_margin="20dp"/>

<DatePicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/datePicker"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"/>

</LinearLayout>

</ScrollView>

It looks as follows :它看起来如下:

在此处输入图片说明

The problem is when the user has to pick a date from the date picker, the scroll input sometimes goes to the enclosing scroll view (I have scroll view as the linear layout proves too long for the screen size I am testing on).问题是当用户必须从日期选择器中选择一个日期时,滚动输入有时会转到封闭的滚动视图(我有滚动视图,因为线性布局证明对于我正在测试的屏幕尺寸来说太长了)。

How can I make sure that when user scrolls over date picker that the entire layout doesn't scroll ?如何确保当用户滚动日期选择器时整个布局不会滚动?

I just extended the DatePicker class and override the onInterceptTouchEvent as one member here shared.我只是扩展了 DatePicker 类并覆盖了 onInterceptTouchEvent 作为这里共享的一个成员。 https://stackoverflow.com/a/9687545 https://stackoverflow.com/a/9687545

public class CustomDatePicker extends DatePicker {


        public CustomDatePicker(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev)
        {
            if (ev.getActionMasked() == MotionEvent.ACTION_DOWN)
            {
                ViewParent p = getParent();
                if (p != null)
                    p.requestDisallowInterceptTouchEvent(true);
            }

            return false;
        }
    }

Then you just use it like this:然后你就可以这样使用它:

<pt.example.appYoumade.CustomDatePicker
           android:id="@+id/docEnd_datePicker"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:calendarViewShown="false"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM