我正在创建一个Android应用,我使用DatePickerDialog
和TimePickerDialog
来获取用户的特定时间,并RadioButton
知道是否符合要求。如果时间超过3天,少于3周并且检查RadioButton
“是”,我想激活一个按钮。
我使用DialogFragment
来获取用户的日期和时间以及更新布局。
MainActivity:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void onRadioButtonClicked(View v) {
// Is the button now checked?
boolean checked = ((RadioButton) v).isChecked();
// Check which radio button was clicked
switch (v.getId()) {
case R.id.yes:
if (checked)
test_button.setEnabled(true);
test_button.setAlpha(1);
break;
case R.id.no:
if (checked)
test_button.setEnabled(false);
test_button.setAlpha(.5f);
break;
}
}
DatePickerFragment:
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
Log.w("DatePicker", "Date = " + year + month + year);
// Update date in the view
((TextView) getActivity().findViewById(R.id.dateTextView)).setText(day + " / " + month);
}
...
}
这适用于RadioButton
,但我无法理解如何将日期和时间作为条件的一部分。每当我更改日期,时间或条件时,必须评估按钮是否将被激活或停用。
有什么建议吗?
答案 0 :(得分:0)
完美日期场景的简单java示例
//after 3 days date
Calendar cal3Days=Calendar.getInstance();
cal3Days.add(Calendar.DAY_OF_YEAR, 3);
//after 3 weeks date
Calendar cal3Weeks=Calendar.getInstance();
cal3Weeks.add(Calendar.WEEK_OF_YEAR, 3);
//after 3 weeks and plus 1 day
Calendar cal3Weeks1day=Calendar.getInstance();
cal3Weeks1day.add(Calendar.WEEK_OF_YEAR, 3);
cal3Weeks1day.add(Calendar.DAY_OF_YEAR, 1);
//your selected date
Calendar selectedDate=Calendar.getInstance();
selectedDate.add(Calendar.WEEK_OF_YEAR, 3);//change int value to test
selectedDate.add(Calendar.DAY_OF_YEAR, 3);//change int value to test
if(selectedDate.after(cal3Days) && selectedDate.before(cal3Weeks) && !selectedDate.after(cal3Weeks1day)){
System.out.println("Yes after 3 days and before 3 weeks");//valid selection
}else{
System.out.println("No Valid selection");
}
答案 1 :(得分:0)
我使用DatePickerFragmentListener
解决了这个问题。当MainActivity中的片段onDateSet
更改日期或时间时,将更新时间和日期,并检查它是否在时间间隔内> 4小时< 2周。
按钮状态将相应更改,如果用户选择了间隔之外的日期或未选中复选框,则使用Toast。
DatePickerFragment:
public interface DatePickerFragmentListener {
void onDateSet(Calendar cal);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
// If you're alive you can't be born in the future
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month, day, 0, 0);
Log.w("DatePicker", "Date = " + year + month + year);
// Update date in the view
((TextView) getActivity().findViewById(R.id.dateTextView)).setText(
cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH ) + " " + day);
// Update listeners
DatePickerFragmentListener activity = (DatePickerFragmentListener) getActivity();
activity.onDateSet(cal);
this.dismiss();
}
MainActivity:
private Calendar birthDate;
private Calendar birthDay;
private Calendar birthTime;
private Calendar cal4Hours;
private Calendar cal2Weeks;
@Override
public void onDateSet(Calendar cal) {
this.birthDay = cal;
this.birthDay.clear(Calendar.HOUR_OF_DAY);
this.birthDay.clear(Calendar.HOUR);
this.birthDay.clear(Calendar.MINUTE);
calculateBirthDate();
updateButtonState();
}
@Override
public void onTimeSet(Calendar cal) {
this.birthTime = cal;
calculateBirthDate();
updateButtonState();
}
public void calculateBirthDate() {
if (this.birthDay == null || this.birthTime == null) {
// Do nothing
} else {
this.birthDate = (Calendar) this.birthDay.clone();
this.birthDate.set(Calendar.HOUR_OF_DAY, this.birthTime.get(Calendar.HOUR_OF_DAY));
this.birthDate.set(Calendar.HOUR, this.birthTime.get(Calendar.HOUR));
this.birthDate.set(Calendar.AM_PM, this.birthTime.get(Calendar.AM_PM));
this.birthDate.set(Calendar.MINUTE, this.birthTime.get(Calendar.MINUTE));
}
}
public boolean isBirthDateWithinRange() {
// Reset from and to range
this.cal4Hours = (Calendar) birthDate.clone();
this.cal2Weeks = (Calendar) birthDate.clone();
cal4Hours.add(Calendar.HOUR_OF_DAY, 4);
cal2Weeks.add(Calendar.WEEK_OF_YEAR, 2);
Calendar now = Calendar.getInstance();
if (now.after(cal4Hours) && now.before(cal2Weeks)) {
return true;
} else {
return false;
}
}
public void updateButtonState() {
if (birthDay == null || birthTime == null || lowBirthWeight == null) {
// do nothing
test_button.setEnabled(false);
test_button.setAlpha(.5f);
} else if (!isBirthDateWithinRange()) {
Toast.makeText(this, "Your child is too young or old to use the app",
Toast.LENGTH_LONG).show();
test_button.setEnabled(false);
test_button.setAlpha(.5f);
} else if (lowBirthWeight == true) {
Toast.makeText(this,
"The birth weight has to be more than 2.5 kg for the app to work",
Toast.LENGTH_LONG).show();
test_button.setEnabled(false);
test_button.setAlpha(.5f);
} else {
// Enable button
test_button.setEnabled(true);
test_button.setAlpha(1);
}
}