gpt4 book ai didi

android - 在 MaterialDateTimePicker 中禁用每个月的星期日

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:25 25 4
gpt4 key购买 nike

我正在使用 MaterialDateTimePicker .我想禁用每个月的特定工作日(如星期日)。它在这个 post 中提到我们可以使用这个库来做到这一点,但是 如何禁用特定的工作日? 我当然可以使用可用的短代码来限制月数,但我不想限制月数只​​是禁用工作日选择。

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

private TextView dateTextView;
private CheckBox modeDarkDate;
private CheckBox vibrateDate;
private CheckBox dismissDate;
private CheckBox limitDates;

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

// Find our View instances
dateTextView = (TextView)findViewById(R.id.date_textview);
Button dateButton = (Button)findViewById(R.id.date_button);


modeDarkDate = (CheckBox)findViewById(R.id.mode_dark_date);
vibrateDate = (CheckBox) findViewById(R.id.vibrate_date);
dismissDate = (CheckBox) findViewById(R.id.dismiss_date);
limitDates = (CheckBox) findViewById(R.id.limit_dates);

// check if picker mode is specified in Style.xml
modeDarkDate.setChecked(Utils.isDarkTheme(this, modeDarkDate.isChecked()));


// Show a datepicker when the dateButton is clicked
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setThemeDark(modeDarkDate.isChecked());
dpd.vibrate(vibrateDate.isChecked());
dpd.dismissOnPause(dismissDate.isChecked());


if (limitDates.isChecked()) {
/* Calendar[] dates = new Calendar[13];
for(int i = -6; i <= 6; i++) {
Calendar date = Calendar.getInstance();
date.add(Calendar.MONTH, i);
dates[i+6] = date;
} */

// Add code here to disbale weekdays like Sundays.





// dpd.setSelectableDays(dates);
}

dpd.show(getFragmentManager(), "Datepickerdialog");
}
});
}

@Override
public void onResume() {
super.onResume();
DatePickerDialog dpd = (DatePickerDialog) getFragmentManager().findFragmentByTag("Datepickerdialog");

if(dpd != null) dpd.setOnDateSetListener(this);
}



@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
dateTextView.setText(date);
}
}

最佳答案

这个答案可能有点晚了,但我有一个可以帮助其他人的解决方案。请尝试以下操作:

    // Calendar saturday;
Calendar sunday;
List<Calendar> weekends = new ArrayList<>();
int weeks = 5;

for (int i = 0; i < (weeks * 7) ; i = i + 7) {
sunday = Calendar.getInstance();
sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
// saturday = Calendar.getInstance();
// saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
// weekends.add(saturday);
weekends.add(sunday);
}
Calendar[] disabledDays = weekends.toArray(new Calendar[weekends.size()]);
dpd.setDisabledDays(disabledDays);

这段代码禁用了接下来的5个星期天,如果你想做更长的时间,只需要修改周。如果您也想禁用星期六,只需取消注释这些行即可。

如果你想在之前的 5 个星期天都这样做,只需将 for 循环修改为:

    for (int i = 0; i < (weeks * 7); i = i + 7) {
for(int j =0; j > (weeks*7) ; j = j - 7);
sunday = Calendar.getInstance();
sunday.add(Calendar.DAY_OF_YEAR, (Calendar.SUNDAY - sunday.get(Calendar.DAY_OF_WEEK) + 7 + i));
// saturday = Calendar.getInstance();
// saturday.add(Calendar.DAY_OF_YEAR, (Calendar.SATURDAY - saturday.get(Calendar.DAY_OF_WEEK) + i));
// weekends.add(saturday);
weekends.add(sunday);
}

因此第二个 for 循环将用于禁用前 5 个星期日。

关于android - 在 MaterialDateTimePicker 中禁用每个月的星期日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37248732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com