我有一个将DatePicker和TimePicker转发器包装到单个视图寻呼机视图中的类,以便用户可以选择日期和时间。此方法返回日历对象。
public class DialogUtils {
public static Calendar showDateAndTimeDialog(final Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_date_time,null, false);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
final TimePicker timePicker = (TimePicker) view.findViewById(R.id.time_picker);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager);
final Calendar calendar = Calendar.getInstance();
viewPager.setAdapter(new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
return viewPager.getChildAt(position);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Date";
case 1:
return "Time";
}
return super.getPageTitle(position);
}
@Override
public int getCount() {
return viewPager.getChildCount();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
final TabLayout layoutTab = (TabLayout) view.findViewById(R.id.layout_tab);
layoutTab.setupWithViewPager(viewPager);
final AlertDialog dateAndTimeDialog = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton("OK", null)
.setNegativeButton("Cancel", null)
.create();
dateAndTimeDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
/*
We override the default BUTTON_POSITIVE OnClickListener to prevent the dialog
from dismissing when the user presses OK
*/
dateAndTimeDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(1);
return;
}
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
// We must dismiss the dialog as we have overriden the default behavior
dialog.dismiss();
}
});
}
});
dateAndTimeDialog.show();
return calendar;
}
}
这在我的Activity类中有两个地方调用。第一个
public Calendar launchDateAndTimePicker(View view) {
return DialogUtils.showDateAndTimeDialog(this);
}
以及在我的Activity中的onCreate
方法中,如点击侦听器
mEditDueDate = ((EditText) findViewById(R.id.task_due_date));
mEditDueDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal;
cal = launchDateAndTimePicker(v);
// Bitmask used to determine timestamp format
int displayMask = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
// Resultant timestamp
String timestamp = DateUtils.formatDateTime(getApplicationContext(), cal.getTimeInMillis(), displayMask);
mEditDueDate.setText(timestamp);
}
});
}
即使我选择了新的日期和时间,我的EditText字段也会显示当前的日期和时间。我觉得我初始化一个日历对象是如何破坏Picker小部件设置的值。我应该如何从showDateAndTimeDialog
返回的日历对象中提取日历值?
编辑解决方案
我更新了DialogUtils类,在其构造函数中获取了一个Activity,并直接更新了EditText字段。
public Activity editActivity;
public static EditText mEditDueDate;
public DialogUtils(Activity editActivity) {
this.editActivity = editActivity;
}
然后找到EditText字段
mEditDueDate = (EditText) editActivity.findViewById(R.id.task_due_date);
以后更新EditText字段。
// Bitmask used to determine timestamp format
int displayMask = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR;
String timestamp = DateUtils.formatDateTime(editActivity.getApplicationContext(), calendar.getTimeInMillis(), displayMask);
mEditDueDate.setText(timestamp);
// We must dismiss the dialog as we have overriden the default behavior
dialog.dismiss();
答案 0 :(得分:1)
截取时间戳并将其设置为EditText
时,用户尚未输入日期。
你应该移动
// Resultant timestamp
String timestamp = DateUtils.formatDateTime(getApplicationContext(), cal.getTimeInMillis(), displayMask);
mEditDueDate.setText(timestamp);
到DialogInterface.OnClickListener.onClick
可能在周围的类中实现。
当您构建对话框时,将侦听器添加到对话框的正按钮:
final AlertDialog dateAndTimeDialog = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton("OK", this)
.setNegativeButton("Cancel", null)
.create();
当然,您将在OnClickListener中提供结果Calendar
对象。
答案 1 :(得分:0)
在设置所选数据之前,您将返回日历。
像这样尝试异步。
public class DialogUtils {
public static void showDateAndTimeDialog(final Context context, final DateCallback dateCallback) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_date_time,null, false);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
final TimePicker timePicker = (TimePicker) view.findViewById(R.id.time_picker);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.view_pager);
final Calendar calendar = Calendar.getInstance();
viewPager.setAdapter(new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
return viewPager.getChildAt(position);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Date";
case 1:
return "Time";
}
return super.getPageTitle(position);
}
@Override
public int getCount() {
return viewPager.getChildCount();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
});
final TabLayout layoutTab = (TabLayout) view.findViewById(R.id.layout_tab);
layoutTab.setupWithViewPager(viewPager);
final AlertDialog dateAndTimeDialog = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton("OK", null)
.setNegativeButton("Cancel", null)
.create();
dateAndTimeDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(final DialogInterface dialog) {
/*
We override the default BUTTON_POSITIVE OnClickListener to prevent the dialog
from dismissing when the user presses OK
*/
dateAndTimeDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(1);
return;
}
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
// We must dismiss the dialog as we have overriden the default behavior
dialog.dismiss();
dateCallback.onDateSelected(calendar);
}
});
}
});
dateAndTimeDialog.show();
}
private interface DateCallback {
void onDateSelected(Calendar calendar);
}
}