📌  相关文章
📜  如何在 Android 中的 Datepicker 中禁用以前或未来的日期?

📅  最后修改于: 2022-05-13 01:55:29.755000             🧑  作者: Mango

如何在 Android 中的 Datepicker 中禁用以前或未来的日期?

在本文中,我们将在 Datepicker 中禁用以前或将来的日期。大多数情况下,当我们在 Android 中使用 DatePicker 时,我们会看到其中的所有日期都已启用。我们可以选择任何日期。但在这里我们将看到如何禁用过去或未来的日期。当我们将任务分配给某人时,禁用过去日期会很有用。那么所选日期必须在未来。当我们要求某人的出生日期时,禁用上一个日期会很有用。那么所选日期必须是过去的日期。让我们实现这个功能。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

步骤 2:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。



XML


 
    
     


Java
import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
 
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
 
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    DatePickerDialog datePicker;
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // initialising the calendar
        final Calendar calendar = Calendar.getInstance();
         
        // initialising the layout
        editText = findViewById(R.id.editext);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
         
        // initialising the datepickerdialog
        datePicker = new DatePickerDialog(MainActivity.this);
         
        // click on edittext to set the value
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(android.widget.DatePicker view, int year, int month, int dayOfMonth) {
                        // adding the selected date in the edittext
                        editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
                    }
                }, year, month, day);
                 
                // set maximum date to be selected as today
                datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
                 
                // show the dialog
                datePicker.show();
            }
        });
    }
}


Java
import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
 
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
 
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    DatePickerDialog datePicker;
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // initialising the calendar
        final Calendar calendar = Calendar.getInstance();
 
        // initialising the layout
        editText = findViewById(R.id.editext);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
 
        // initialising the datepickerdialog
        datePicker = new DatePickerDialog(MainActivity.this);
 
        // click on edittext to set the value
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(android.widget.DatePicker view, int year, int month, int dayOfMonth) {
                        // adding the selected date in the edittext
                        editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
                    }
                }, year, month, day);
 
                // set maximum date to be selected as today
                datePicker.getDatePicker().setMinDate(calendar.getTimeInMillis());
 
                // show the dialog
                datePicker.show();
            }
        });
    }
}


第 3 步:使用 MainActivity。 Java文件

转到主活动。 Java文件,参考如下代码。

对于最大日期:

使用getDatePicker()从 DatePickerDialog 获取 DatePicker。使用setMaxDate()将最大日期设置为当前日期:

datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());

下面是MainActivity的代码。 Java文件。

Java



import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
 
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
 
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    DatePickerDialog datePicker;
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // initialising the calendar
        final Calendar calendar = Calendar.getInstance();
         
        // initialising the layout
        editText = findViewById(R.id.editext);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
         
        // initialising the datepickerdialog
        datePicker = new DatePickerDialog(MainActivity.this);
         
        // click on edittext to set the value
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(android.widget.DatePicker view, int year, int month, int dayOfMonth) {
                        // adding the selected date in the edittext
                        editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
                    }
                }, year, month, day);
                 
                // set maximum date to be selected as today
                datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
                 
                // show the dialog
                datePicker.show();
            }
        });
    }
}

对于最小日期:

同样,使用getDatePicker()从 DatePickerDialog 获取 DatePicker。使用setMinDate()将最小日期设置为当前日期:   

datePicker.getDatePicker().setMinDate(calendar.getTimeInMillis());

下面是MainActivity的代码。 Java文件。

Java

import android.app.DatePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
 
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
 
import java.util.Calendar;
 
public class MainActivity extends AppCompatActivity {
 
    EditText editText;
    DatePickerDialog datePicker;
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // initialising the calendar
        final Calendar calendar = Calendar.getInstance();
 
        // initialising the layout
        editText = findViewById(R.id.editext);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
 
        // initialising the datepickerdialog
        datePicker = new DatePickerDialog(MainActivity.this);
 
        // click on edittext to set the value
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(android.widget.DatePicker view, int year, int month, int dayOfMonth) {
                        // adding the selected date in the edittext
                        editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
                    }
                }, year, month, day);
 
                // set maximum date to be selected as today
                datePicker.getDatePicker().setMinDate(calendar.getTimeInMillis());
 
                // show the dialog
                datePicker.show();
            }
        });
    }
}

输出: