java RPickerDialog易于使用和管理,并获得不同的日期格式,以便快速开发DatePicker
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java RPickerDialog易于使用和管理,并获得不同的日期格式,以便快速开发DatePicker相关的知识,希望对你有一定的参考价值。
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
/**
* Created by root on 28/2/18.
*/
public class RPickerDialog {
public static SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
public static SimpleDateFormat formatTime24 = new SimpleDateFormat("HH:mm:ss");
public static SimpleDateFormat formatTime12 = new SimpleDateFormat("hh:mm a");
private static Context context;
private static RPickerDialog instance = new RPickerDialog();
private static Calendar calendar;
private String anything = null;
private boolean is24Hour = false;
private RPickerDialog() {}
public static RPickerDialog get(Context context) {
if (instance == null)
instance = new RPickerDialog();
if (calendar == null)
calendar = Calendar.getInstance(Locale.getDefault());
instance.context = context;
return instance;
}
public RDatePicker datePicker(@Nullable final String anything) {
this.anything= anything;
return new RDatePicker();
}
public RTimePicker timePicker(@Nullable final String anything, boolean is24Hour) {
this.anything = anything;
this.is24Hour = is24Hour;
return new RTimePicker();
}
public RTimePicker timePicker(@Nullable final String anything) {
this.anything = anything;
this.is24Hour = false;
return new RTimePicker();
}
/**
* This class will provide Date Picker functionality
*/
public class RDatePicker {
private onSelectListener listener;
private SimpleDateFormat dateFormat;
private Calendar calDate;
public RDatePicker() {
calDate = Calendar.getInstance();
}
public RDatePicker addListener(onSelectListener listener) {
this.listener = listener;
return this;
}
public RDatePicker format(SimpleDateFormat dateFormat) {
this.dateFormat = dateFormat;
return this;
}
public void show() {
new DatePickerDialog(
context,
onDateSetListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
).show();
}
DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
calDate.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
if (listener == null) {
Toast.makeText(context, "Please attach the listener", Toast.LENGTH_SHORT).show();
} else if (dateFormat == null){
listener.onSelection(
calDate.getTime().toString(),
formatDate.format(calDate.getTime()),
anything
);
} else {
listener.onSelection(
dateFormat.format(calDate.getTime()),
formatDate.format(calDate.getTime()),
anything
);
}
}
};
}
/**
* This class will provide Time Picker functionality
*/
public class RTimePicker {
private onSelectListener listener;
private Calendar calTime;
public RTimePicker() {
calTime = Calendar.getInstance();
}
public RTimePicker addListener(onSelectListener listener) {
this.listener = listener;
return this;
}
public void show() {
new TimePickerDialog(
context,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24Hour
).show();
}
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
calTime.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
i,
i1
);
if (listener == null) {
Toast.makeText(context, "Please attach the listener", Toast.LENGTH_SHORT).show();
} else {
listener.onSelection(
formatTime12.format(calTime.getTime()),
formatTime24.format(calTime.getTime()),
anything
);
}
}
};
}
public interface onSelectListener {
void onSelection(String formattedValue, String forDatabase, @Nullable String myAnything);
}
}
以上是关于java RPickerDialog易于使用和管理,并获得不同的日期格式,以便快速开发DatePicker的主要内容,如果未能解决你的问题,请参考以下文章
DHTMLX Vault-JavaScript文件上传库:易于使用的控件,用于直观地上传和管理各种文件类型