Android时间区间的选择刚做完的⼀个简单的选择时间段的⼩控件。
效果图:
坛子岭
选择推送时间段
关键代码实现:
~~~
public class TimeRangePickerDialogextends Dialog {
private Contextcontext;
private String startTime;
private String endTime;
private int screenWidth;
private TimePicker timePickerStart;
private TimePicker timePickerEnd;
机警的意思
private View cancelBtn, submitBtn;
private ConfirmAction confirmAction;
public TimeRangePickerDialog(Context context) {
super(context);
}
public TimeRangePickerDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener);
}
public TimeRangePickerDialog(Context context, int theme) {
水滴的故事super(context, theme);
}
public TimeRangePickerDialog(Context context, String startAndEndTime, ConfirmAction confirmAction) { super(context, R.style.dialog);
List strings = RegEx(startAndEndTime, "\\d+:\\d+");
if (!CommonUtils.isNull(strings) && strings.size() >=2) {
this.startTime = RegEx(startAndEndTime, "\\d+:\\d+").get(0);
}
DisplayMetrics metrics = Resources().getDisplayMetrics();
screenWidth = metrics.widthPixels -getDensityValue(80, context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_time_range_picker, null);
tContentView(view);
getWindow().tLayout(screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
initView();
initData();
tEvent();
}
private void initView() {
timePickerStart = (TimePicker) findViewById(R.id.timePickerStart);
timePickerEnd = (TimePicker) findViewById(R.id.timePickerEnd);
cancelBtn = findViewById(R.id.cancelBtn);
submitBtn = findViewById(R.id.submitBtn);
}
private void initData() {
timePickerStart.tIs24HourView(true);
timePickerEnd.tIs24HourView(true);环境伦理
timePickerStart.tDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
timePickerEnd.tDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
tTimePickerDividerColor(timePickerStart);
tTimePickerDividerColor(timePickerEnd);
if (!CommonUtils.isNull(startTime) && !CommonUtils.isNull(endTime)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
timePickerStart.tHour(Integer.parInt(startTime.substring(0, startTime.indexOf(":"))));
timePickerStart.tMinute(Integer.parInt(startTime.substring(startTime.indexOf(":") +1)));三不限岗位
timePickerEnd.tHour(Integer.parInt(endTime.substring(0, endTime.indexOf(":"))));
timePickerEnd.tMinute(Integer.parInt(endTime.substring(endTime.indexOf(":") +1)));
}el {
timePickerStart.tCurrentHour(Integer.parInt(startTime.substring(0, startTime.indexOf(":"))));
timePickerStart.tCurrentMinute(Integer.parInt(startTime.substring(startTime.indexOf(":") +1)));
timePickerEnd.tCurrentHour(Integer.parInt(endTime.substring(0, endTime.indexOf(":"))));
timePickerEnd.tCurrentMinute(Integer.parInt(endTime.substring(endTime.indexOf(":") +1))); }
}
timePickerStart.tOnTimeChangedListener((timePicker, hourOfDay, minute) -> {
String h = hourOfDay <10 ?"0" + hourOfDay :"" + hourOfDay;
String m = minute <10 ?"0" + minute :"" + minute;
startTime = h +":" + m;
});
timePickerEnd.tOnTimeChangedListener((timePicker, hourOfDay, minute) -> {
String h = hourOfDay <10 ?"0" + hourOfDay :"" + hourOfDay;康师傅橙汁
String m = minute <10 ?"0" + minute :"" + minute;
endTime = h +":" + m;
});
世界最低点
}
private void tEvent() {
cancelBtn.tOnClickListener(v -> {
dismiss();
});
submitBtn.tOnClickListener(v -> {
dismiss();
});
this.tCanceledOnTouchOutside(true);
}
private void tTimePickerDividerColor(TimePicker timePicker) {
LinearLayout llFirst = (LinearLayout) ChildAt(0);
LinearLayout mSpinners = (LinearLayout) ChildAt(1);
for (int i =0; i < ChildCount(); i++) {
if (ChildAt(i)instanceof NumberPicker) {
Field[] pickerFields = DeclaredFields();
tPickerMargin((NumberPicker) ChildAt(i));
建设学习型for (Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.tAccessible(true);
try {
pf.ChildAt(i), new ColorDrawable());
}catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (Resources.NotFoundException e) {
e.printStackTrace();
}catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
}
}
/**
* 设置picker之间的间距
*/
private void tPickerMargin(NumberPicker picker) {
LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) LayoutParams(); p.tMargins(-getDensityValue(16, context), 0, -getDensityValue(16, context), 0); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
p.tMarginStart(-getDensityValue(16, context));