我使用 DatePicker fragment 从用户那里获取日期。
...
c = Calendar.getInstance();
year = c.get(Calendar.YEAR) ;
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog picker = new DatePickerDialog(getActivity(),
this, year, month, day);
return picker;
public void onDateSet(DatePicker view, int year, int month, int day) {
final EditText txtDate;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,day);
txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
String myFormat = "MM/dd/yy" + " "; \\ *** formatted with "yy" to fit on EditText line rather than "yyyy"
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
txtDate.setText(sdf.format(calendar.getTime()));
}
在 MainActivity 中,我想获取原始的 DatePicker 日期,比如在 EditText 行上设置的 2017 年 2 月 28 日,并在用户单击日期进行编辑时将其作为当前日期放回 DatePicker fragment 中。 EditText 行将日期显示为年份的“yy”,因此为 02/28/17。我假设我需要将 EditText 数据从“yy”转换回“yyyy”以将日期部分放入一个包中,然后将日期设置到 DatePicker fragment 中。
fListenerEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
// Extract the previously entered date from the EditText line as a string
String dateStr = fListenerEditText.getText().toString().replace(" ", "");
String myFormat2 = "MM/dd/yyyy" + " "; \\ *** I assume I need to put "yyyy" back into the DatePicker not "yy" so need to re-format the Date
SimpleDateFormat sdf2 = new SimpleDateFormat(myFormat2, Locale.US);
String formattedDate = sdf2.format(dateStr); \\ *** wrong here?
String[]dateParts = formattedDate.split("/");
c.set(Integer.parseInt(dateParts[2]),Integer.parseInt(dateParts[0])-1,Integer.parseInt(dateParts[1]));
Bundle argsbundle = new Bundle();
argsbundle.putInt("year", c.get(Calendar.YEAR));
argsbundle.putInt("month", c.get(Calendar.MONTH));
argsbundle.putInt("day", c.get(Calendar.DAY_OF_MONTH));
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setArguments(argsbundle);
newFragment.show(getSupportFragmentManager(), "datePicker");
当我点击 EditText 行时,DatePicker 按预期打开,但我没有得到之前的日期,我得到的是 1900 年 1 月 1 日。我在这里缺少什么格式?
最佳答案
您可以使用 updateDate(yourSelectedDate)
方法更新您的 DatePickerDialog
,如下所示:
在您的Activity
类中创建DatePickerDialog
的全局实例:
public class CustomDatePicker extends Activity implements DatePickerDialog.OnDateSetListener {
private DatePickerDialog mDatePickerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
//....
Calendar uCal = Calendar.getInstance();
mDatePickerDialog = new DatePickerDialog(this,
this,
uCal.get(Calendar.YEAR),
uCal.get(Calendar.MONTH),
uCal.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
mDatePickerDialog.updateDate(year, month, day);
}
}
之后,使用 onDateSet
方法更新您的 mDPDialog
值(selectedYear、selectedMonth 和 selectedDay)
关于android - 如何 bundle 以前的 DatePicker 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42525673/