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

时间:2019-03-06 15:00:17

标签: android android-datepicker

我有两个对话框选择器:“签入”和“签出”。但我希望在单击“签到”中的日期后,它将禁用之前的日期。因此,当我在“签到”中单击2019年3月6日时,在“结帐”中应为2019年3月7日,依此类推。

签入和签出图像1

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);
    }
}

2 个答案:

答案 0 :(得分:0)

我相信您正在寻找的是重新整理日期选择器代码,以便可以在创建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);
    }
}

然后在onCreateDialog()的{​​{1}}中使用上面我原始答案中的代码。

让我知道这是否有意义!

答案 1 :(得分:0)

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

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);
}
}
}

Fragment类

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;
} }

我早些时候开始尝试使用“标签”属性;可能不是采用适当的字帖样式,但可以达到我的目的。

通过这种方式,您似乎在(main)活动中实现了侦听器,但在那里没有看到“ @override”。您能解释一下吗?