datepicker:点击按钮跳转到一年中不同的月份

iezvtpos  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(347)

我使用的是一个没有箭头的日期选择器,因为我想跳到任何一个月,当我为它们单击特定的按钮。
例如,如果我单击march,就让我跳到datepicker上的march。
我使用textview来输出月份和年份,尽管我觉得这是一个糟糕的解决方案。
下面的代码是我检查出类似答案后整理出来的,但结果很差。有些月份我不能点击日期,每个月的天数和日期都是错误的。
下面我将用代码、xml和图像来最好地说明我所说的内容。
下图:[在此处输入图像描述][1]
xml格式

<DatePicker
                    android:id="@+id/datePicker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:theme="@style/CalenderViewDateCustomText"
                    android:layout_marginTop="-100dp"
                    android:layout_centerHorizontal="true" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="0dp"
                    android:layout_marginEnd="0dp"
                    android:background="@drawable/whiteblock" />

            </RelativeLayout>

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/selectTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true"
                android:layout_marginTop="680dp"
                android:layout_marginEnd="0dp"
                android:text="Select a Time"
                android:textAlignment="center"
                android:fontFamily="@font/lato_bold"
                android:textSize="20sp"
                android:textColor="#202224"/>

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/dateoutput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="July 2020"
                android:layout_marginTop="331dp"
                android:fontFamily="@font/josefinsans_semibold"
                android:textSize="20dp"
                android:textColor="#202224"
                android:layout_marginStart="16dp"/>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="316dp"
                android:background="@drawable/mask3x"/>

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/registerheading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="54dp"
                android:fontFamily="@font/josefin_sans_bold"
                android:gravity="center_horizontal"
                android:text="Schedule"
                android:textColor="#202224"
                android:textSize="20dp"/>

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ca_arrow"
                android:layout_width="34.5dp"
                android:layout_height="26.5dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="35dp"
                android:layout_marginTop="56.75dp"
                android:background="@drawable/left_arrow"/>

            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="610dp"
                android:fillViewport="true"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginStart="16dp">

                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/jan"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="Jan"
                        android:textColor="#979797"
                        android:textSize="12dp"/>

                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/feb"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="feb"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/march"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="March"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/April"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="April"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/May"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="May"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/June"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="June"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/July"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="July"
                        android:textColor="#979797"
                        android:textSize="12dp"/>

                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/August"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="August"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/September"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="September"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/October"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="October"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/November"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="November"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                    <androidx.appcompat.widget.AppCompatButton
                        android:id="@+id/December"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:text="December"
                        android:textColor="#979797"
                        android:textSize="12dp"/>
                </LinearLayout>
            </HorizontalScrollView>

时间表.java

Calendar calendar = Calendar.getInstance();

        timepicker();
        SimpleDateFormat month_date = new SimpleDateFormat("MMMM yyyy");
        String date = month_date.format(calendar.getTime());
        outputdate.setText(date);

        datePicker.init(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH), calendar.get(calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            }
        });

        jan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar jan = Calendar.getInstance();
                jan.set(2020, 1, 0, 0, 0, 0);
                datePicker.setMinDate(jan.getTimeInMillis());

                Calendar janlast = Calendar.getInstance();
                janlast.set(2020, 2, 0, 0, 0, 0);
                datePicker.setMaxDate(janlast.getTimeInMillis());
                outputdate.setText("January 2020");
            }
        });
        feb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar feb = Calendar.getInstance();
                feb.set(2020, 1, 0, 0, 0, 0);
                datePicker.setMinDate(feb.getTimeInMillis());
                Calendar feblast = Calendar.getInstance();
                feblast.set(2020, 2, 0, 0, 0, 0);
                datePicker.setMaxDate(feblast.getTimeInMillis());
                outputdate.setText("February 2020");
            }
        });
        march.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar march = Calendar.getInstance();
                march.set(2020, 2, 0, 0, 0, 0);
                datePicker.setMinDate(march.getTimeInMillis());

                Calendar marchlast = Calendar.getInstance();
                marchlast.set(2020, 3, 0, 0, 0, 0);
                datePicker.setMaxDate(marchlast.getTimeInMillis());
                outputdate.setText("March 2020");
            }
        });
        april.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar april = Calendar.getInstance();
                april.set(2020, 4, 0, 0, 0, 0);
                datePicker.setMinDate(april.getTimeInMillis());

                Calendar aprillast = Calendar.getInstance();
                aprillast.set(2020, 5, 0, 0, 0, 0);
                datePicker.setMaxDate(aprillast.getTimeInMillis());
                outputdate.setText("April 2020");
            }
        });
        may.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar may = Calendar.getInstance();
                may.set(2020, 5, 1, 0, 0, 0);
                datePicker.setMinDate(may.getTimeInMillis());

                Calendar maylast = Calendar.getInstance();
                maylast.set(2020, 5, 31, 0, 0, 0);
                datePicker.setMaxDate(maylast.getTimeInMillis());
                outputdate.setText("May 2020");
            }
        });
        june.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar june = Calendar.getInstance();
                june.set(2020, 6, 1, 0, 0, 0);
                datePicker.setMinDate(june.getTimeInMillis());

                Calendar junelast = Calendar.getInstance();
                junelast.set(2020, 6, 1, 0, 0, 0);
                datePicker.setMaxDate(junelast.getTimeInMillis());
                outputdate.setText("June 2020");
            }
        });
        july.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar july = Calendar.getInstance();
                july.set(2020, 7, 1, 0, 0, 0);
                datePicker.setMinDate(july.getTimeInMillis());

                Calendar julylast = Calendar.getInstance();
                julylast.set(2020, 7, 1, 0, 0, 0);
                datePicker.setMaxDate(julylast.getTimeInMillis());
                outputdate.setText("July 2020");
            }
        });
        august.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar august = Calendar.getInstance();
                august.set(2020, 8, 1, 0, 0, 0);
                datePicker.setMinDate(august.getTimeInMillis());

                Calendar augustlast = Calendar.getInstance();
                augustlast.set(2020, 8, 1, 0, 0, 0);
                datePicker.setMaxDate(augustlast.getTimeInMillis());
                outputdate.setText("August 2020");
            }
        });
        september.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar september = Calendar.getInstance();
                september.set(2020, 9, 1, 0, 0, 0);
                datePicker.setMinDate(september.getTimeInMillis());

                Calendar septemberlast = Calendar.getInstance();
                septemberlast.set(2020, 9, 1, 0, 0, 0);
                datePicker.setMaxDate(septemberlast.getTimeInMillis());
                outputdate.setText("September 2020");
            }
        });
        october.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar october = Calendar.getInstance();
                october.set(2020, 10, 1, 0, 0, 0);
                datePicker.setMinDate(october.getTimeInMillis());

                Calendar octoberlast = Calendar.getInstance();
                octoberlast.set(2020, 10, 1, 0, 0, 0);
                datePicker.setMaxDate(octoberlast.getTimeInMillis());
                outputdate.setText("October 2020");
            }
        });

        november.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar november = Calendar.getInstance();
                november.set(2020, 11, 1, 0, 0, 0);
                datePicker.setMinDate(november.getTimeInMillis());

                Calendar novemberlast = Calendar.getInstance();
                novemberlast.set(2020, 11, 1, 0, 0, 0);
                datePicker.setMaxDate(novemberlast.getTimeInMillis());
                outputdate.setText("November 2020");
            }
        });
        december.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar december = Calendar.getInstance();
                december.set(2020, 12, 1, 0, 0, 0);
                datePicker.setMinDate(december.getTimeInMillis());

                Calendar decemberlast = Calendar.getInstance();
                decemberlast.set(2020, 12, 31, 0, 0, 0);
                datePicker.setMaxDate(decemberlast.getTimeInMillis());
                outputdate.setText("December 2020");
            }
        });
    ```

  [1]: https://i.stack.imgur.com/42Yol.png

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题