如何通过使用多天的选择,从我的应用程序
问题描述:
我使用复选框多天的选择如何通过使用多天的选择,从我的应用程序
if (monday == 1) {
chkMonday.setChecked(true);
} if (tuesday == 1) {
chkTuesday.setChecked(true);
} if (wednesday == 1) {
chkWedneday.setChecked(true);
} if (thursday == 1) {
chkThursday.setChecked(true);
} if (friday == 1) {
chkFriday.setChecked(true);
} if (saturday == 1) {
chkSaturday.setChecked(true);
} if (sunday == 1) {
chkSunday.setChecked(true);
}
现在我想通过多次重复天,根据我从选择天的日历重复事件周报复选框。对于我,如果情况是这样
values.put(CalendarContract.Reminders.TITLE, "Routine Custom");
values.put(CalendarContract.Reminders.DTSTART, millisecondsTimesEveryday);
values.put(CalendarContract.Reminders.HAS_ALARM, true);
if (chkMonday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=MO");//UNTIL=1924885800000
}else if (chkTuesday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TU");
} else if (chkWedneday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=WE");
} else if (chkThursday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TH");
} else if (chkFriday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=FR");
} else if (chkSaturday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SA");
} else if (chkSunday.isChecked()) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SU");
}
values.put(CalendarContract.Reminders.DTEND, EndtimeInMilliseconds);
但在日历,只有第一个选择天重复使用的if..else。如何将所有选定日期设置为提醒中的重复日期?
答
您使用If-else检查条件但是这种方法的问题是,一旦第一个条件成立,它就会发生并且从不费心去检查其他条件,问题的解决方案将会迭代通过在检查复选框的布尔状态之前需要创建的整个数据集,我宁愿借助Hashmap来存储复选框的布尔状态,我将存储星期名称并沿着布尔状态配对与它这个持续一周的日子
//Create a Hashmap
HashMap<String,Boolean> dayOfWeek = new HashMap<>();
if (monday == 1) {
chkMonday.setChecked(true);
dayOfWeek.put("Monday",true);
} if (tuesday == 1) {
chkTuesday.setChecked(true);
dayOfWeek.put("Tuesday",true);//pairing an unique string with a boolean value depending on whether it is checked or not
} if (wednesday == 1) {
chkWedneday.setChecked(true);
dayOfWeek.put("Wedneday",true);//pairing continues for all days similarly
} if (thursday == 1) {
chkThursday.setChecked(true);
dayOfWeek.put("Thursday",true);
} if (friday == 1) {
chkFriday.setChecked(true);
dayOfWeek.put("Friday",true);
} if (saturday == 1) {
chkSaturday.setChecked(true);
dayOfWeek.put("Saturday",true);
} if (sunday == 1) {
chkSunday.setChecked(true);
dayOfWeek.put("Sunday",true);
}
创造价值作为一般
values.put(CalendarContract.Reminders.TITLE, "Routine Custom");
values.put(CalendarContract.Reminders.DTSTART, millisecondsTimesEveryday);
values.put(CalendarContract.Reminders.HAS_ALARM, true);
//现在你需要通过HashMap来迭代插入提醒所有选中的日子:
Iterator it = dayOfWeek.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
if (pair.getValue()==true){
if (pair.getKey().toString().equalsIgnoreCase("Monday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=MO");//UNTIL=1924885800000
}else if (pair.getKey().toString().equalsIgnoreCase("Tuesday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TU");
} else if (pair.getKey().toString().equalsIgnoreCase("Wednesday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=WE");
} else if (pair.getKey().toString().equalsIgnoreCase("Thursday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TH");
} else if (pair.getKey().toString().equalsIgnoreCase("Friday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=FR");
} else if (pair.getKey().toString().equalsIgnoreCase("Saturday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SA");
} else if (pair.getKey().toString().equalsIgnoreCase("Sunday")) {
values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SU");
}
}
}
values.put(CalendarContract.Reminders.DTEND, EndtimeInMilliseconds);
使用这款U将能够在整个数据集迭代(哈希映射在这种情况下, )插入所有被检查的日子的提醒
我想这会对你有所帮助。 https://stackoverflow.com/questions/44204387/alarmmanager-setinexactrepeating-setwindow-setrepeating-methods-do-not-fire-al/44205413#44205413 –
我希望它使用CalendarContract系统的日历提醒。 @AniruddhParihar –
你可以再次清楚地解释你的要求.. !! –