从第一个 DatePicker 中选择后如何禁用日期

How to disable dates after selecting from the first DatePicker

我有两个对话框选择器:签入和签出。但我希望在单击“签入”中的日期后,它将禁用之前的日期。所以当我在 Check In 上点击 2019 年 3 月 6 日时,在 Checkout 上,它应该是 2019 年 3 月 7 日,依此类推。

签入和签出图像

DatePickerFragment.Java:

public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {


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

        DatePickerDialog dialog = new DatePickerDialog(getContext(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);

        dialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000);


        return dialog;
    }

Details.Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

     final DialogFragment datePicker = new DatePickerFragment();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            datePicker.show(getSupportFragmentManager(),"Date Picker");
            startDateOrEndDate = true ;
        }
    });
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            datePicker.show(getSupportFragmentManager(),"Date Picker");
            startDateOrEndDate = false ;
        }
    });
      }

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year );
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    String date = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());



    TextView textView = (TextView) findViewById(R.id.checkInTV);
    TextView textView2 = (TextView) findViewById(R.id.checkOutTV);




    if (startDateOrEndDate) {
        textView.setText(date);
    } else {
        textView2.setText(date);
    }
}

我相信您正在寻找的是修改日期选择器代码,以便在创建时可以设置 setMinDate(),这样您就可以传入最短日期。现在您正在此处设置最短日期:

dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

这对于第一个日期选择器来说工作正常,因为给定的最短日期是今天,但是对于第二个日期选择器,您希望将第一个日期作为最晚日期传递。

我不知道你的其他代码在创建日期选择器片段方面是什么样的,但假设你正在使用片段,你可以在创建片段时将最小日期放在参数包中。下面是创建片段时如何传递最小日期的示例:

// This would be the start date the user had previously chosen
long startDate = 0L

Bundle bundle = new Bundle(); 

// We only want to put in the min date if this is the second date
if (!startDateOrEndDate) {
    bundle.putLong("minDate", startDate); 
}

// Create the Fragment, set the bundle as its arguments
DatePickerFragment fragment = new DatePickerFragment(); 
fragment.setArguments(bundle);

然后,在 DatePickerFragment 中,您可以相应地设置最小日期:

public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    // Dialog Creation code here

    // Default the min date to today 
    long minDate = System.currentTimeMillis() - 1000;

    // Check if there are arguments
    Bundle args = getArguments(); 
    if (args != null) {
        // If there are args, grab the min date from it (default to today just in case)
        minDate = args.getLong("minDate", minDate); 
    }

    dialog.getDatePicker().setMinDate(minDate); 

    return dialog; 
}

有几点需要考虑:

  • 您必须确保用户选择的开始日期早于结束日期。
  • 您可能希望将结束日期设置为 (startDate + 1 day) 而不是 startDate,因此在通过 minDate 时需要考虑到这一点。

总的来说,这应该让您了解如何进行。

旁注:我建议将您的布尔值重命名为明确的名称,例如 isStartDate。这样,如果它是真的,您就知道它是开始日期,反之亦然。 startDateOrEndDate 并没有告诉我们它是哪一个,如果它是真的,它只是告诉我们它是开始日期还是结束日期。

编辑 - 2019 年 3 月 11 日

鉴于你的新代码,这就是你如何用我上面所说的来改变它

Details.java

private long startDate = -1L; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment datePicker = new DatePickerFragment();
            datePicker.show(getSupportFragmentManager(),"Date Picker");
            startDateOrEndDate = true ;
        }
    });
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (startDate == -1L) {
                // Start date hasn't been chosen yet, tell user to do so
                Toast.makeText(Details.this, "Please choose start date", Toast.LENGTH_SHORT).show(); 
                return; 
            }

            // Add the start date to the bundle
            Bundle bundle = new Bundle(); 
            bundle.putLong("minDate", startDate); 

            // Create the Fragment, set the bundle as its arguments
            DatePickerFragment datePicker = new DatePickerFragment(); 
            datePicker.setArguments(bundle);
            datePicker.show(getSupportFragmentManager(),"Date Picker");
            startDateOrEndDate = false ;
        }
    });
}

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year );
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    String date = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());

    TextView textView = (TextView) findViewById(R.id.checkInTV);
    TextView textView2 = (TextView) findViewById(R.id.checkOutTV);

    if (startDateOrEndDate) {
        // If this is the start date, save it to the instance variable above
        startDate = c.getTimeinMillis(); 
        textView.setText(date);
    } else {
        textView2.setText(date);
    }
}

并在 DatePickerFragmentonCreateDialog() 中使用上面我的原始答案中的代码。

让我知道这是否有意义!

您可以将以下代码复制粘贴到某处,然后添加您的代码。

MainActivity

public class MainActivity extends AppCompatActivity implements
    DatePickerDialog.OnDateSetListener{
final long[]l = new long[1];
boolean startDateOrEndDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button en = findViewById(R.id.entry);
    Button ex = findViewById(R.id.exit);


    final myDatepickerFragment datePicker = new myDatepickerFragment();

    en.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            l[0]= System.currentTimeMillis();
            datePicker.show(getSupportFragmentManager(),l[0]+"");

            startDateOrEndDate = true ;
        }
    });
    ex.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            datePicker.show(getSupportFragmentManager(),l[0]+"");
            startDateOrEndDate = false ;
        }
    });

}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    l[0]= c.getTimeInMillis()+24*60*60*1000;
    String date = DateFormat.getDateInstance(DateFormat.FULL).format(c.getTime());
    System.out.println(date);

TextView textView = (TextView) findViewById(R.id.checkInTV);
TextView textView2 = (TextView) findViewById(R.id.checkOutTV);


if (startDateOrEndDate) {
    textView.setText(date);
} else {
    textView2.setText(date);
}
}
}

片段class

public class myDatepickerFragment extends DialogFragment {
private String tag;

@Override
public void show(FragmentManager manager, String tag) {
    super.show(manager, tag);
    this.tag = tag;
}

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {


    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    long l = Long.parseLong(tag);
    DatePickerDialog dialog = new DatePickerDialog(getContext(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);

    dialog.getDatePicker().setMinDate(l-1000);
    dialog.getDatePicker().setBackgroundColor(Color.rgb(50,100,100));


    return dialog;
} }

我之前开始尝试 "tag" 属性;可能不是合适的字帖风格,但它符合我的目的。

顺便说一句,您似乎在 (main) activity 中实现了侦听器,但我在那里没有看到“@override”。请你解释一下。