【问题标题】:How to set maximum and minimum date for datepicker in android如何在android中设置日期选择器的最大和最小日期
【发布时间】:2014-09-15 04:54:29
【问题描述】:

我正在使用我在网上找到的 datepicker 教程。但我想为 datepicker 设置最大和最小日期。另外我听说 API 级别低于 11 存在问题。

我的代码:

package com.androidexample.datepicker;

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerExample extends Activity {

    private TextView Output;
    private Button changeDate;

    private int year;
    private int month;
    private int day;

    static final int DATE_PICKER_ID = 1111; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Output = (TextView) findViewById(R.id.Output);
        changeDate = (Button) findViewById(R.id.changeDate);

        // Get current date by calender

        final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);

        // Show current date

        Output.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(month + 1).append("-").append(day).append("-")
                .append(year).append(" "));

        // Button listener to show date picker dialog

        changeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // On button click show datepicker dialog 
                showDialog(DATE_PICKER_ID);

            }

        });
   }


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_ID:

            // open datepicker dialog. 
            // set date picker for current date 
            // add pickerListener listner to date picker
            return new DatePickerDialog(this, pickerListener, year, month,day);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date 
            Output.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

           }
        };

}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/changeDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click To Change Date" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Current/Selected Date (M-D-YYYY): "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/Output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />



</LinearLayout>

上面代码的问题是年份部分没有上限。我还需要年份部分的下限。有人可以告诉我如何修改代码来设置上限和下限年份部分?

【问题讨论】:

  • DatePickersetMinDate()setMaxDate() 方法。
  • 但是我没有在 xml 文件中使用 datepicker 控件。有人可以告诉我如何修改我的代码来设置最大和最小日期吗?
  • @Andrew ,在链接示例中,日期选择器在 xml 文件中定义。但在我的示例中,日期选择器未在 xml 文件中定义。
  • @user3852672 您是否尝试过至少 1 个答案?部分答案与DatePickerDialog有关。

标签: java android eclipse datepicker


【解决方案1】:

DatePickerDialog.OnDateSetListener() 中,您可以将月份、年份、日期与您指定的最小和最大日期进行比较。

【讨论】:

    【解决方案2】:

    您可以使用DatePickerDialog获取DatePicker

    DatePickerDialog dialog = new DatePickerDialog(this, pickerListener, year, month,day);
    //calculate min and max dates (you can use, Calender API or Joda Time, just search for it.
    dialog.getDatePicker().setMaxDate(maxDate.getTime());
    dialog.getDatePicker().setMinDate(minDate.getTime());
    return dialog;
    

    【讨论】:

      【解决方案3】:

      您可以通过简单地调用 getDatePicker() 从 DatePickerDialog 获取底层 DatePicker 并使用以下方法设置其边界:

      setMinDate(long minDate)

      setMaxDate(long maxDate)

      这仅适用于 API 级别 11 及以上。

      【讨论】:

        猜你喜欢
        • 2020-05-31
        • 2016-01-08
        • 1970-01-01
        • 2018-05-22
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多