2个日期选择器作为android中的微调器



我在android中用2个日期选择器作为微调器时遇到了一些问题。第一个是入住日期,第二个是退房日期。当我选择签入日期时,我希望我的签出日期自动跳到第二天,反之亦然。当我选择了签出日期时,签入日期会自动跳到前一天。我尝试了很多,但没有找到这种设置的方法。我总是以一个无限循环结束,因为这个触发了那个,那个又触发了这个,我的应用程序崩溃了。你们有什么解决办法吗?谢谢你的帮助。

解决方案

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<DatePicker
android:id="@+id/checkInDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
<TextView
android:id="@+id/checkInDayOfWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<DatePicker
android:id="@+id/checkOutDatePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:calendarViewShown="false"
android:datePickerMode="spinner" />
<TextView
android:id="@+id/checkOutDayOfWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements DatePicker.OnDateChangedListener {
private DatePicker checkInDatePicker;
private DatePicker checkOutDatePicker;
private TextView checkInDayOfWeekTextView;
private TextView checkOutDayOfWeekTextView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkInDatePicker = findViewById(R.id.checkInDatePicker);
checkOutDatePicker = findViewById(R.id.checkOutDatePicker);
checkInDayOfWeekTextView = findViewById(R.id.checkInDayOfWeek);
checkOutDayOfWeekTextView = findViewById(R.id.checkOutDayOfWeek);
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH);
int dayOfMonth = now.get(Calendar.DAY_OF_MONTH);
checkInDatePicker.init(year, month, dayOfMonth, this);
checkInDayOfWeekTextView.setText(getDayOfWeek(now));
checkOutDatePicker.init(year, month, dayOfMonth, this);
checkOutDayOfWeekTextView.setText(getDayOfWeek(now));
}
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (view.getId() == checkInDatePicker.getId()) {
// Update check in day of week.
Calendar checkInDay = Calendar.getInstance();
checkInDay.set(year, monthOfYear, dayOfMonth);
String checkInDayOfWeek = getDayOfWeek(checkInDay);
checkInDayOfWeekTextView.setText(checkInDayOfWeek);
// Update check out DatePicker
checkOutDatePicker.init(year, monthOfYear, dayOfMonth, null);
Calendar nextDay = getNextDay(year, monthOfYear, dayOfMonth);
checkOutDatePicker.init(nextDay.get(Calendar.YEAR),
nextDay.get(Calendar.MONTH),
nextDay.get(Calendar.DAY_OF_MONTH),
this);
// Update check out day of week.
String checkOutDayOfWeek = getDayOfWeek(nextDay);
checkOutDayOfWeekTextView.setText(checkOutDayOfWeek);
} else {
// Update check out day of week.
Calendar checkOutDay = Calendar.getInstance();
checkOutDay.set(year, monthOfYear, dayOfMonth);
String checkOutDayOfWeek = getDayOfWeek(checkOutDay);
checkOutDayOfWeekTextView.setText(checkOutDayOfWeek);
// Update check in DatePicker
checkInDatePicker.init(year, monthOfYear, dayOfMonth, null);
Calendar prevDay = getPreviousDay(year, monthOfYear, dayOfMonth);
checkInDatePicker.init(
prevDay.get(Calendar.YEAR),
prevDay.get(Calendar.MONTH),
prevDay.get(Calendar.DAY_OF_MONTH),
this);
// Update check in day of week.
String checkInDayOfWeek = getDayOfWeek(prevDay);
checkInDayOfWeekTextView.setText(checkInDayOfWeek);
}
}
private Calendar getNextDay(int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar;
}
private Calendar getPreviousDay(int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar;
}
private String getDayOfWeek(Calendar calendar) {
String dayOfWeek = "";
switch (calendar.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY:
dayOfWeek = "Sunday";
break;
case Calendar.MONDAY:
dayOfWeek = "Monday";
break;
case Calendar.TUESDAY:
dayOfWeek = "Tuesday";
break;
case Calendar.WEDNESDAY:
dayOfWeek = "Wednesday";
break;
case Calendar.THURSDAY:
dayOfWeek = "Thursday";
break;
case Calendar.FRIDAY:
dayOfWeek = "Friday";
break;
case Calendar.SATURDAY:
dayOfWeek = "Saturday";
break;
}
return dayOfWeek;
}
}

相关内容

  • 没有找到相关文章

最新更新