如果开始日期在给定范围内并且单选按钮为 "yes",我如何激活按钮?
How do I activate a button if the date from is within a given range and if radio button is "yes"?
我正在制作一个 Android 应用程序,我在其中使用 DatePickerDialog
和 TimePickerDialog
从用户那里获取特定时间,并使用 RadioButton
了解需求是否是遇见了。如果时间大于 3 天,小于 3 周并且 RadioButton
"yes" 被选中,我想激活一个按钮。
我使用 DialogFragment
s 从用户那里获取日期和时间以及更新布局。
主要活动:
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
,但我无法理解如何将日期和时间作为条件的一部分。每次我更改日期、时间或条件时,它都必须评估按钮是要激活还是停用。
有什么建议吗?
完美约会场景的简单 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");
}
我用 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();
}
主要活动:
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);
}
}
我正在制作一个 Android 应用程序,我在其中使用 DatePickerDialog
和 TimePickerDialog
从用户那里获取特定时间,并使用 RadioButton
了解需求是否是遇见了。如果时间大于 3 天,小于 3 周并且 RadioButton
"yes" 被选中,我想激活一个按钮。
我使用 DialogFragment
s 从用户那里获取日期和时间以及更新布局。
主要活动:
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
,但我无法理解如何将日期和时间作为条件的一部分。每次我更改日期、时间或条件时,它都必须评估按钮是要激活还是停用。
有什么建议吗?
完美约会场景的简单 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");
}
我用 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();
}
主要活动:
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);
}
}