首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将日期选择器文本框日期限制为未来日期。它应该只允许以前的日期

如何将日期选择器文本框日期限制为未来日期。它应该只允许以前的日期
EN

Stack Overflow用户
提问于 2013-02-15 19:28:41
回答 4查看 1.7K关注 0票数 1

我想限制日期选择器将日期设置为高于系统当前日期。告诉我如何验证日期选择器日期。

EN

回答 4

Stack Overflow用户

发布于 2013-02-15 20:51:59

代码语言:javascript
复制
Create Custom Dialog an show toat on date greater then current date


final Dialog dialog = new Dialog(SettingActivity.this);
                dialog.setContentView(R.layout.date_picker_view);
                dialog.setTitle("Select Date");
                final TextView tvTime = (TextView) dialog
                        .findViewById(R.id.tv_time);
                final DatePicker datePicker = (DatePicker) dialog
                        .findViewById`enter code here`(R.id.datePicker1);
                Button set = (Button) dialog.findViewById(R.id.button_time_set);
                Button cancel = (Button) dialog
                        .findViewById(R.id.button_time_cancel);

                datePicker
                        .setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

                Calendar  originalDate=Calendar.getInstance();
                 Calendar previousWeekDay = (Calendar) originalDate.clone();
                  previousWeekDay.add(Calendar.YEAR, -35);
                  previousWeekDay.set(Calendar.MONTH, 0);
                  previousWeekDay.set(Calendar.DAY_OF_MONTH, 1);

                  datePicker.init(previousWeekDay.get(Calendar.YEAR), previousWeekDay.get(Calendar.MONTH), previousWeekDay.get(Calendar.DAY_OF_MONTH), null);

                final Calendar calendar = Calendar.getInstance();
                final int mDay = calendar
                        .get(Calendar.DAY_OF_MONTH)+1;

                calendar.set(Calendar.DAY_OF_MONTH, mDay);




                set.setOnClickListener(new OnClickListener() {



                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                      Calendar cal=Calendar.getInstance();
                      cal.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());

                      if(cal.after(calendar)){
                        Toast.makeText(SettingActivity.this, "DOB not more then current date", Toast.LENGTH_SHORT).show();
                          handler.etDOB.setText("");
                      }else{
                             date_selected = String.valueOf(datePicker.getDayOfMonth())+"/"+String.valueOf(datePicker.getMonth()+1)+"/"+String.valueOf(datePicker.getYear());

                          handler.etDOB.setText(datePicker.getDayOfMonth()+"-"+AccessRecordForPDF.MONTHS[datePicker.getMonth()+1]+"-"+String.valueOf(datePicker.getYear()));
                          dialog.dismiss();
                      }






                    }
                });
                cancel.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                dialog.show();


            }
        });


And create dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="270dp"
    android:layout_height="190dp"
    android:background="@android:color/black"
    android:orientation="vertical" >


    <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



    <!--
         <com.amvrin.PillsScheduler.DrumPicker.DateDrumPicker
        android:id="@+id/datepicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    -->

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_time_set"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/button_green"
            android:text="Set"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/button_time_cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/button_green"
            android:text="Cancel"
            android:textColor="@android:color/white" />
    </LinearLayout>

</LinearLayout>
票数 1
EN

Stack Overflow用户

发布于 2013-07-26 13:54:13

代码语言:javascript
复制
package com.example.pickerdate;

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.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView mDateDisplay;
    private int mYear;
    private int year1;
    private int mMonth;
    private int mDay;
    private int month;
    private int day;
    static final int DATE_DIALOG_ID = 1;
    Button pickDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        pickDate = (Button) findViewById(R.id.pickDate);
        pickDate.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                System.out.println("hello3");
                showDialog(DATE_DIALOG_ID);

            }
        });

        System.out.println("hello1");
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();
        year1 = mYear;
        month = mMonth;
        day = mDay;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        DatePickerDialog _date = null;
        switch (id) {

        case DATE_DIALOG_ID:
            _date = new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                    mDay) {
                @Override
                public void onDateChanged(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
                    System.out.println("----------onDateChanged()-----------"
                            + mYear + "--" + year);
                    System.out.println("----------onDateChanged()-----------"
                            + mMonth + "--" + monthOfYear);
                    System.out.println("----------onDateChanged()-----------"
                            + mDay + "--" + dayOfMonth);

                    /*
                     * These lines of commented code used for only setting the
                     * maximum date on Date Picker..
                     * 
                     * if (year > mYear && year) view.updateDate(mYear, mMonth,
                     * mDay);
                     * 
                     * if (monthOfYear > mMonth && year == mYear )
                     * view.updateDate(mYear, mMonth, mDay);
                     * 
                     * if (dayOfMonth > mDay && year == mYear && monthOfYear ==
                     * mMonth) view.updateDate(mYear, mMonth, mDay);
                     */

                    // these below lines of code used for setting the maximum as
                    // well as minimum dates on Date Picker Dialog..

                    if ((mYear > year)
                            || ((mMonth > monthOfYear) && (mYear == year))
                            || ((mDay > dayOfMonth) && (mYear == year) && (mMonth == monthOfYear))) {
                        view.updateDate(year1, month, day);

                    }

                }
            };

        }
        System.out.println("hello5");
        return _date;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {

        case DATE_DIALOG_ID:
            System.out.println("hello6");

            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
            break;
        }
    }

    private void updateDisplay() {
        System.out.println("hello2");
        mDateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-").append(mDay).append("-")
                .append(mYear).append(" "));
    }

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

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            System.out.println("hello7");

            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            System.out.println("year=" + year);
            System.out.println("month=" + monthOfYear);
            System.out.println("day=" + dayOfMonth);
            updateDisplay();
        }
    };
}
票数 0
EN

Stack Overflow用户

发布于 2013-07-26 14:08:55

自API级别11以来,有一种方法可以做到这一点:

1.DatePicker.setMaxDate(长maxDate)

2.如果它必须在以前的版本中工作,请使用以下方法:

public void init(int year,int monthOfYear,int dayOfMonth,DatePicker.OnDateChangedListener onDateChangedListener)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14893703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档