1:在房间设置的时候,展示营业时间
2:修改选择时间展示选择的日期 3:修改了群组全成员展示接界面
This commit is contained in:
@@ -129,6 +129,9 @@ public class RoomBean implements Serializable {
|
||||
private int is_pk; //1:接收 2:不接受
|
||||
private int last_pk_room_id;//记录上次pk的房间id
|
||||
|
||||
private String start_time="";//营业时间的开始时间
|
||||
private String end_time="";//营业时间的结束时间
|
||||
|
||||
public int getSceneId() {
|
||||
if (sound_effect != null) {
|
||||
return sound_effect.getId();
|
||||
|
||||
@@ -80,6 +80,7 @@ public class ConfirmDialog extends Dialog {
|
||||
window.setGravity(Gravity.CENTER); // 居中显示
|
||||
window.setBackgroundDrawableResource(R.drawable.bg_r16_fff); // 透明背景
|
||||
}
|
||||
setCanceledOnTouchOutside(false); // 设置点击外部不取消对话框
|
||||
}
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TabHost;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -14,16 +15,43 @@ import androidx.annotation.Nullable;
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
|
||||
import com.xscm.moduleutil.R;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
|
||||
private OnTimeRangeSelectedListener listener;
|
||||
private boolean showTime = true; // 默认显示时分秒
|
||||
private boolean showDate = true; // 默认显示日期
|
||||
private boolean showSeconds = true; // 默认显示秒
|
||||
|
||||
// 格式模板
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
private static final String TIME_FORMAT_SECONDS = "HH:mm:ss";
|
||||
private static final String TIME_FORMAT_NO_SECONDS = "HH:mm";
|
||||
private static final String DATETIME_FORMAT_SECONDS = "yyyy-MM-dd HH:mm:ss";
|
||||
private static final String DATETIME_FORMAT_NO_SECONDS = "yyyy-MM-dd HH:mm";
|
||||
|
||||
private SimpleDateFormat dateFormat;
|
||||
private SimpleDateFormat timeFormat;
|
||||
private SimpleDateFormat dateTimeFormat;
|
||||
|
||||
// UI组件
|
||||
private TextView tvStartTime;
|
||||
private TextView tvEndTime;
|
||||
private TabHost tabHost;
|
||||
private WheelDatePicker wheelStartDate;
|
||||
private WheelTimePicker wheelStartTime;
|
||||
private WheelDatePicker wheelEndDate;
|
||||
private WheelTimePicker wheelEndTime;
|
||||
|
||||
// 时间数据
|
||||
private Calendar startCalendar;
|
||||
private Calendar endCalendar;
|
||||
private View view;
|
||||
|
||||
public interface OnTimeRangeSelectedListener {
|
||||
void onTimeRangeSelected(Date startDate, Date endDate);
|
||||
}
|
||||
@@ -31,7 +59,7 @@ public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
public void setOnTimeRangeSelectedListener(OnTimeRangeSelectedListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置是否显示时分秒
|
||||
* @param showTime true显示年月日时分秒,false只显示年月日
|
||||
@@ -39,7 +67,7 @@ public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
public void setShowTime(boolean showTime) {
|
||||
this.showTime = showTime;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置是否显示日期
|
||||
* @param showDate true显示日期,false只显示时间
|
||||
@@ -47,7 +75,7 @@ public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
public void setShowDate(boolean showDate) {
|
||||
this.showDate = showDate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置是否显示秒
|
||||
* @param showSeconds true显示秒,false不显示秒
|
||||
@@ -55,7 +83,7 @@ public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
public void setShowSeconds(boolean showSeconds) {
|
||||
this.showSeconds = showSeconds;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建实例
|
||||
* @param showTime true显示年月日时分秒,false只显示年月日
|
||||
@@ -66,7 +94,7 @@ public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
fragment.setShowTime(showTime);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建实例
|
||||
* @param showTime true显示年月日时分秒,false只显示年月日
|
||||
@@ -86,140 +114,309 @@ public class DoubleTimePickerBottomSheet extends BottomSheetDialogFragment {
|
||||
return new CustomBottomSheetDialog(requireContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// 初始化格式化工具
|
||||
initFormats();
|
||||
// 初始化时间数据
|
||||
initCalendars();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化日期时间格式化工具
|
||||
*/
|
||||
private void initFormats() {
|
||||
dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
|
||||
|
||||
if (showSeconds) {
|
||||
timeFormat = new SimpleDateFormat(TIME_FORMAT_SECONDS, Locale.getDefault());
|
||||
dateTimeFormat = new SimpleDateFormat(DATETIME_FORMAT_SECONDS, Locale.getDefault());
|
||||
} else {
|
||||
timeFormat = new SimpleDateFormat(TIME_FORMAT_NO_SECONDS, Locale.getDefault());
|
||||
dateTimeFormat = new SimpleDateFormat(DATETIME_FORMAT_NO_SECONDS, Locale.getDefault());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化时间数据
|
||||
*/
|
||||
private void initCalendars() {
|
||||
startCalendar = Calendar.getInstance();
|
||||
endCalendar = Calendar.getInstance();
|
||||
|
||||
// 设置默认结束时间略晚于开始时间
|
||||
if (showTime) {
|
||||
endCalendar.add(Calendar.HOUR, 1);
|
||||
} else {
|
||||
endCalendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置获取显示格式
|
||||
*/
|
||||
private String getFormattedTime(Calendar calendar) {
|
||||
if (showDate && showTime) {
|
||||
return dateTimeFormat.format(calendar.getTime());
|
||||
} else if (showDate && !showTime) {
|
||||
return dateFormat.format(calendar.getTime());
|
||||
} else if (!showDate && showTime) {
|
||||
return timeFormat.format(calendar.getTime());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新顶部TextView显示
|
||||
*/
|
||||
private void updateDisplayText() {
|
||||
if (tvStartTime != null) {
|
||||
tvStartTime.setText(getFormattedTime(startCalendar));
|
||||
}
|
||||
if (tvEndTime != null) {
|
||||
tvEndTime.setText(getFormattedTime(endCalendar));
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.dialog_double_time_picker, container, false);
|
||||
view = inflater.inflate(R.layout.dialog_double_time_picker, container, false);
|
||||
|
||||
// 初始化 TabHost
|
||||
TabHost tabHost = view.findViewById(R.id.tabHost);
|
||||
initViews(view);
|
||||
setupWheelPickers();
|
||||
setupTabHost();
|
||||
setupButtons();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化视图组件
|
||||
*/
|
||||
private void initViews(View view) {
|
||||
tvStartTime = view.findViewById(R.id.tv_start);
|
||||
tvEndTime = view.findViewById(R.id.tv_end);
|
||||
tabHost = view.findViewById(R.id.tabHost);
|
||||
|
||||
wheelStartDate = view.findViewById(R.id.wheel_start_date);
|
||||
wheelStartTime = view.findViewById(R.id.wheel_start_time);
|
||||
wheelEndDate = view.findViewById(R.id.wheel_end_date);
|
||||
wheelEndTime = view.findViewById(R.id.wheel_end_time);
|
||||
|
||||
// 初始化显示文本
|
||||
updateDisplayText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置滚轮选择器
|
||||
*/
|
||||
private void setupWheelPickers() {
|
||||
// 根据showDate和showTime参数控制日期和时间选择器的显示和布局
|
||||
adjustPickerVisibility();
|
||||
|
||||
// 初始化开始时间
|
||||
if (showDate) {
|
||||
wheelStartDate.init(startCalendar.get(Calendar.YEAR),
|
||||
startCalendar.get(Calendar.MONTH),
|
||||
startCalendar.get(Calendar.DAY_OF_MONTH));
|
||||
wheelStartDate.setOnDateSelectedListener((year, month, day) -> {
|
||||
startCalendar.set(year, month, day);
|
||||
updateDisplayText();
|
||||
});
|
||||
}
|
||||
|
||||
if (showTime) {
|
||||
wheelStartTime.init(startCalendar.get(Calendar.HOUR_OF_DAY),
|
||||
startCalendar.get(Calendar.MINUTE),
|
||||
showSeconds ? startCalendar.get(Calendar.SECOND) : 0);
|
||||
wheelStartTime.setShowSeconds(showSeconds);
|
||||
wheelStartTime.setOnValueChangedListener(() -> {
|
||||
startCalendar.set(Calendar.HOUR_OF_DAY, wheelStartTime.getHour());
|
||||
startCalendar.set(Calendar.MINUTE, wheelStartTime.getMinute());
|
||||
if (showSeconds) {
|
||||
startCalendar.set(Calendar.SECOND, wheelStartTime.getSecond());
|
||||
}
|
||||
updateDisplayText();
|
||||
});
|
||||
}
|
||||
|
||||
// 初始化结束时间
|
||||
if (showDate) {
|
||||
wheelEndDate.init(endCalendar.get(Calendar.YEAR),
|
||||
endCalendar.get(Calendar.MONTH),
|
||||
endCalendar.get(Calendar.DAY_OF_MONTH));
|
||||
wheelEndDate.setOnDateSelectedListener((year, month, day) -> {
|
||||
endCalendar.set(year, month, day);
|
||||
updateDisplayText();
|
||||
});
|
||||
}
|
||||
|
||||
if (showTime) {
|
||||
wheelEndTime.init(endCalendar.get(Calendar.HOUR_OF_DAY),
|
||||
endCalendar.get(Calendar.MINUTE),
|
||||
showSeconds ? endCalendar.get(Calendar.SECOND) : 0);
|
||||
wheelEndTime.setShowSeconds(showSeconds);
|
||||
wheelEndTime.setOnValueChangedListener(() -> {
|
||||
endCalendar.set(Calendar.HOUR_OF_DAY, wheelEndTime.getHour());
|
||||
endCalendar.set(Calendar.MINUTE, wheelEndTime.getMinute());
|
||||
if (showSeconds) {
|
||||
endCalendar.set(Calendar.SECOND, wheelEndTime.getSecond());
|
||||
}
|
||||
updateDisplayText();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调整选择器显示状态和布局
|
||||
*/
|
||||
private void adjustPickerVisibility() {
|
||||
if (showDate && showTime) {
|
||||
// 显示日期和时间时,使用默认布局
|
||||
setPickerVisibility(View.VISIBLE, View.VISIBLE, View.VISIBLE, View.VISIBLE);
|
||||
resetPickerLayoutParams(wheelStartDate, wheelEndDate);
|
||||
} else if (showDate && !showTime) {
|
||||
// 只显示日期时,隐藏时间选择器并让日期选择器铺满宽度
|
||||
setPickerVisibility(View.VISIBLE, View.GONE, View.VISIBLE, View.GONE);
|
||||
setPickerFullWidth(wheelStartDate, wheelEndDate);
|
||||
} else if (!showDate && showTime) {
|
||||
// 只显示时间时,隐藏日期选择器并让时间选择器铺满宽度
|
||||
setPickerVisibility(View.GONE, View.VISIBLE, View.GONE, View.VISIBLE);
|
||||
setPickerFullWidth(wheelStartTime, wheelEndTime);
|
||||
}
|
||||
}
|
||||
|
||||
private void setPickerVisibility(int startDateVis, int startTimeVis,
|
||||
int endDateVis, int endTimeVis) {
|
||||
wheelStartDate.setVisibility(startDateVis);
|
||||
wheelStartTime.setVisibility(startTimeVis);
|
||||
wheelEndDate.setVisibility(endDateVis);
|
||||
wheelEndTime.setVisibility(endTimeVis);
|
||||
}
|
||||
|
||||
private void resetPickerLayoutParams(WheelDatePicker... pickers) {
|
||||
for (WheelDatePicker picker : pickers) {
|
||||
ViewGroup.LayoutParams params = picker.getLayoutParams();
|
||||
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
picker.setLayoutParams(params);
|
||||
}
|
||||
}
|
||||
|
||||
private void setPickerFullWidth(View... views) {
|
||||
for (View view : views) {
|
||||
ViewGroup.LayoutParams params = view.getLayoutParams();
|
||||
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
view.setLayoutParams(params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置TabHost
|
||||
*/
|
||||
private void setupTabHost() {
|
||||
tabHost.setup();
|
||||
|
||||
tabHost.addTab(tabHost.newTabSpec("start").setIndicator("开始时间").setContent(R.id.start_time_tab));
|
||||
tabHost.addTab(tabHost.newTabSpec("end").setIndicator("结束时间").setContent(R.id.end_time_tab));
|
||||
|
||||
// 初始化 Wheel Views
|
||||
WheelDatePicker wheelStartDate = view.findViewById(R.id.wheel_start_date);
|
||||
WheelTimePicker wheelStartTime = view.findViewById(R.id.wheel_start_time);
|
||||
WheelDatePicker wheelEndDate = view.findViewById(R.id.wheel_end_date);
|
||||
WheelTimePicker wheelEndTime = view.findViewById(R.id.wheel_end_time);
|
||||
|
||||
// 根据showDate和showTime参数控制日期和时间选择器的显示和布局
|
||||
if (showDate && showTime) {
|
||||
// 显示日期和时间时,使用默认布局
|
||||
wheelStartDate.setVisibility(View.VISIBLE);
|
||||
wheelStartTime.setVisibility(View.VISIBLE);
|
||||
wheelEndDate.setVisibility(View.VISIBLE);
|
||||
wheelEndTime.setVisibility(View.VISIBLE);
|
||||
|
||||
// 重置日期选择器的布局参数为默认值
|
||||
ViewGroup.LayoutParams startDateParams = wheelStartDate.getLayoutParams();
|
||||
startDateParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
wheelStartDate.setLayoutParams(startDateParams);
|
||||
|
||||
ViewGroup.LayoutParams endDateParams = wheelEndDate.getLayoutParams();
|
||||
endDateParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
|
||||
wheelEndDate.setLayoutParams(endDateParams);
|
||||
} else if (showDate && !showTime) {
|
||||
// 只显示日期时,隐藏时间选择器并让日期选择器铺满宽度
|
||||
wheelStartTime.setVisibility(View.GONE);
|
||||
wheelEndTime.setVisibility(View.GONE);
|
||||
|
||||
// 设置日期选择器铺满宽度
|
||||
ViewGroup.LayoutParams startDateParams = wheelStartDate.getLayoutParams();
|
||||
startDateParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
wheelStartDate.setLayoutParams(startDateParams);
|
||||
|
||||
ViewGroup.LayoutParams endDateParams = wheelEndDate.getLayoutParams();
|
||||
endDateParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
wheelEndDate.setLayoutParams(endDateParams);
|
||||
} else if (!showDate && showTime) {
|
||||
// 只显示时间时,隐藏日期选择器并让时间选择器铺满宽度
|
||||
wheelStartDate.setVisibility(View.GONE);
|
||||
wheelStartTime.setVisibility(View.VISIBLE);
|
||||
wheelEndDate.setVisibility(View.GONE);
|
||||
wheelEndTime.setVisibility(View.VISIBLE);
|
||||
tabHost.setOnTabChangedListener(tabId -> {
|
||||
if ("start".equals(tabId)) {
|
||||
restoreStartTimeSelection();
|
||||
} else if ("end".equals(tabId)) {
|
||||
restoreEndTimeSelection();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 设置时间选择器铺满宽度
|
||||
ViewGroup.LayoutParams startTimeParams = wheelStartTime.getLayoutParams();
|
||||
startTimeParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
wheelStartTime.setLayoutParams(startTimeParams);
|
||||
|
||||
ViewGroup.LayoutParams endTimeParams = wheelEndTime.getLayoutParams();
|
||||
endTimeParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
wheelEndTime.setLayoutParams(endTimeParams);
|
||||
}
|
||||
|
||||
// 默认设置当前时间
|
||||
Calendar startCal = Calendar.getInstance();
|
||||
/**
|
||||
* 恢复开始时间选择
|
||||
*/
|
||||
private void restoreStartTimeSelection() {
|
||||
if (showDate) {
|
||||
wheelStartDate.init(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), startCal.get(Calendar.DAY_OF_MONTH));
|
||||
wheelStartDate.init(startCalendar.get(Calendar.YEAR),
|
||||
startCalendar.get(Calendar.MONTH),
|
||||
startCalendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
if (showTime) {
|
||||
wheelStartTime.init(startCal.get(Calendar.HOUR_OF_DAY), startCal.get(Calendar.MINUTE), showSeconds ? startCal.get(Calendar.SECOND) : 0);
|
||||
wheelStartTime.setShowSeconds(showSeconds);
|
||||
wheelStartTime.init(startCalendar.get(Calendar.HOUR_OF_DAY),
|
||||
startCalendar.get(Calendar.MINUTE),
|
||||
showSeconds ? startCalendar.get(Calendar.SECOND) : 0);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置默认结束时间略晚于开始时间
|
||||
Calendar endCal = (Calendar) startCal.clone();
|
||||
if (showTime) {
|
||||
endCal.add(Calendar.HOUR, 1);
|
||||
} else {
|
||||
endCal.add(Calendar.DAY_OF_MONTH, 1);
|
||||
}
|
||||
/**
|
||||
* 恢复结束时间选择
|
||||
*/
|
||||
private void restoreEndTimeSelection() {
|
||||
if (showDate) {
|
||||
wheelEndDate.init(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH), endCal.get(Calendar.DAY_OF_MONTH));
|
||||
wheelEndDate.init(endCalendar.get(Calendar.YEAR),
|
||||
endCalendar.get(Calendar.MONTH),
|
||||
endCalendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
if (showTime) {
|
||||
wheelEndTime.init(endCal.get(Calendar.HOUR_OF_DAY), endCal.get(Calendar.MINUTE), showSeconds ? endCal.get(Calendar.SECOND) : 0);
|
||||
wheelEndTime.setShowSeconds(showSeconds);
|
||||
wheelEndTime.init(endCalendar.get(Calendar.HOUR_OF_DAY),
|
||||
endCalendar.get(Calendar.MINUTE),
|
||||
showSeconds ? endCalendar.get(Calendar.SECOND) : 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置按钮事件
|
||||
*/
|
||||
private void setupButtons() {
|
||||
// 取消按钮
|
||||
view.findViewById(R.id.btn_cancel).setOnClickListener(v -> dismiss());
|
||||
|
||||
// 确定按钮
|
||||
view.findViewById(R.id.btn_sure).setOnClickListener(v -> {
|
||||
Calendar start = Calendar.getInstance();
|
||||
Calendar end = Calendar.getInstance();
|
||||
|
||||
if (showDate && showTime) {
|
||||
// 显示日期和时间
|
||||
start.set(wheelStartDate.getYear(), wheelStartDate.getMonth(), wheelStartDate.getDay(),
|
||||
wheelStartTime.getHour(), wheelStartTime.getMinute(), wheelStartTime.getSecond());
|
||||
end.set(wheelEndDate.getYear(), wheelEndDate.getMonth(), wheelEndDate.getDay(),
|
||||
wheelEndTime.getHour(), wheelEndTime.getMinute(), wheelEndTime.getSecond());
|
||||
} else if (showDate && !showTime) {
|
||||
// 只显示日期
|
||||
start.set(wheelStartDate.getYear(), wheelStartDate.getMonth(), wheelStartDate.getDay(),
|
||||
0, 0, 0);
|
||||
end.set(wheelEndDate.getYear(), wheelEndDate.getMonth(), wheelEndDate.getDay(),
|
||||
23, 59, 59);
|
||||
} else if (!showDate && showTime) {
|
||||
// 只显示时间,使用当前日期
|
||||
int currentYear = start.get(Calendar.YEAR);
|
||||
int currentMonth = start.get(Calendar.MONTH);
|
||||
int currentDay = start.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
start.set(currentYear, currentMonth, currentDay,
|
||||
wheelStartTime.getHour(), wheelStartTime.getMinute(), wheelStartTime.getSecond());
|
||||
end.set(currentYear, currentMonth, currentDay,
|
||||
wheelEndTime.getHour(), wheelEndTime.getMinute(), wheelEndTime.getSecond());
|
||||
}
|
||||
if ((showDate && showTime) || (showDate && !showTime) ) {
|
||||
if (end.getTime().before(start.getTime())) {
|
||||
Toast.makeText(requireContext(), "结束时间不能早于开始时间", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (listener != null) {
|
||||
listener.onTimeRangeSelected(start.getTime(), end.getTime());
|
||||
}
|
||||
dismiss();
|
||||
validateAndSubmit();
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证并提交选择
|
||||
*/
|
||||
private void validateAndSubmit() {
|
||||
// 确保使用最新的选择器值
|
||||
syncPickerValuesToCalendars();
|
||||
|
||||
// 验证时间范围
|
||||
if ((showDate && showTime) || (showDate && !showTime)) {
|
||||
if (endCalendar.getTime().before(startCalendar.getTime())) {
|
||||
Toast.makeText(requireContext(), "结束时间不能早于开始时间", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 回调监听器
|
||||
if (listener != null) {
|
||||
listener.onTimeRangeSelected(startCalendar.getTime(), endCalendar.getTime());
|
||||
}
|
||||
dismiss();
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步选择器值到Calendar对象
|
||||
*/
|
||||
private void syncPickerValuesToCalendars() {
|
||||
if (showDate) {
|
||||
startCalendar.set(wheelStartDate.getYear(), wheelStartDate.getMonth(), wheelStartDate.getDay());
|
||||
endCalendar.set(wheelEndDate.getYear(), wheelEndDate.getMonth(), wheelEndDate.getDay());
|
||||
}
|
||||
|
||||
if (showTime) {
|
||||
startCalendar.set(Calendar.HOUR_OF_DAY, wheelStartTime.getHour());
|
||||
startCalendar.set(Calendar.MINUTE, wheelStartTime.getMinute());
|
||||
if (showSeconds) {
|
||||
startCalendar.set(Calendar.SECOND, wheelStartTime.getSecond());
|
||||
}
|
||||
|
||||
endCalendar.set(Calendar.HOUR_OF_DAY, wheelEndTime.getHour());
|
||||
endCalendar.set(Calendar.MINUTE, wheelEndTime.getMinute());
|
||||
if (showSeconds) {
|
||||
endCalendar.set(Calendar.SECOND, wheelEndTime.getSecond());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,13 @@ public class WheelDatePicker extends LinearLayout {
|
||||
|
||||
private NumberPicker yearPicker, monthPicker, dayPicker;
|
||||
private int maxDay = 31;
|
||||
|
||||
// 值变化监听器接口
|
||||
public interface OnDateSelectedListener {
|
||||
void onDateSelected(int year, int month, int day);
|
||||
}
|
||||
|
||||
private OnDateSelectedListener onDateSelectedListener;
|
||||
|
||||
public WheelDatePicker(Context context) {
|
||||
this(context, null);
|
||||
@@ -45,11 +52,28 @@ public class WheelDatePicker extends LinearLayout {
|
||||
// 月份从 1 到 12
|
||||
monthPicker.setMinValue(1);
|
||||
monthPicker.setMaxValue(12);
|
||||
monthPicker.setOnValueChangedListener((picker, oldVal, newVal) -> updateDays());
|
||||
monthPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||
updateDays();
|
||||
if (onDateSelectedListener != null) {
|
||||
onDateSelectedListener.onDateSelected(yearPicker.getValue(), monthPicker.getValue() - 1, dayPicker.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
// 默认天数范围
|
||||
dayPicker.setMinValue(1);
|
||||
dayPicker.setMaxValue(maxDay);
|
||||
dayPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||
if (onDateSelectedListener != null) {
|
||||
onDateSelectedListener.onDateSelected(yearPicker.getValue(), monthPicker.getValue() - 1, dayPicker.getValue());
|
||||
}
|
||||
});
|
||||
|
||||
yearPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||
updateDays();
|
||||
if (onDateSelectedListener != null) {
|
||||
onDateSelectedListener.onDateSelected(yearPicker.getValue(), monthPicker.getValue() - 1, dayPicker.getValue());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateDays() {
|
||||
@@ -94,4 +118,12 @@ public class WheelDatePicker extends LinearLayout {
|
||||
public int getDay() {
|
||||
return dayPicker.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置日期选择监听器
|
||||
* @param listener 监听器
|
||||
*/
|
||||
public void setOnDateSelectedListener(OnDateSelectedListener listener) {
|
||||
this.onDateSelectedListener = listener;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,13 @@ public class WheelTimePicker extends LinearLayout {
|
||||
|
||||
private NumberPicker hourPicker, minutePicker, secondPicker;
|
||||
private boolean showSeconds = true; // 默认显示秒
|
||||
|
||||
// 值变化监听器接口
|
||||
public interface OnValueChangedListener {
|
||||
void onValueChanged();
|
||||
}
|
||||
|
||||
private OnValueChangedListener onValueChangedListener;
|
||||
|
||||
public WheelTimePicker(Context context) {
|
||||
this(context, null);
|
||||
@@ -41,14 +48,29 @@ public class WheelTimePicker extends LinearLayout {
|
||||
// 设置小时范围
|
||||
hourPicker.setMinValue(0);
|
||||
hourPicker.setMaxValue(23);
|
||||
hourPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||
if (onValueChangedListener != null) {
|
||||
onValueChangedListener.onValueChanged();
|
||||
}
|
||||
});
|
||||
|
||||
// 设置分钟范围
|
||||
minutePicker.setMinValue(0);
|
||||
minutePicker.setMaxValue(59);
|
||||
minutePicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||
if (onValueChangedListener != null) {
|
||||
onValueChangedListener.onValueChanged();
|
||||
}
|
||||
});
|
||||
|
||||
// 秒范围
|
||||
secondPicker.setMinValue(0);
|
||||
secondPicker.setMaxValue(59);
|
||||
secondPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||
if (onValueChangedListener != null) {
|
||||
onValueChangedListener.onValueChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void init(int hour, int minute, int second) {
|
||||
@@ -79,4 +101,12 @@ public class WheelTimePicker extends LinearLayout {
|
||||
secondPicker.setVisibility(showSeconds ? VISIBLE : GONE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置值变化监听器
|
||||
* @param listener 监听器
|
||||
*/
|
||||
public void setOnValueChangedListener(OnValueChangedListener listener) {
|
||||
this.onValueChangedListener = listener;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,30 @@
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="@dimen/dp_10"
|
||||
android:orientation="horizontal">
|
||||
<TextView
|
||||
android:id="@+id/tv_start"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="@dimen/sp_12" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_end"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:gravity="center"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="@dimen/sp_12" />
|
||||
</LinearLayout>
|
||||
|
||||
<TabHost
|
||||
android:id="@+id/tabHost"
|
||||
android:layout_width="match_parent"
|
||||
|
||||
Reference in New Issue
Block a user