您好,我需要帮助。
我有MainActivity
,PickerDialogs
和DateSettings
,我希望从EditText
获取DatePickerDialog
的日期。
我该怎么做?非常感谢。
以下是java类:
DateSettings.java
public class DateSettings implements DatePickerDialog.OnDateSetListener
{
Context context;
public DateSettings(Context context)
{
this.context = context;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
Toast.makeText(context, "Selected date: " +monthOfYear+ " / " +dayOfMonth+ " / " +year, Toast.LENGTH_SHORT).show();
}
}
PickerDialogs.java
public class PickerDialogs extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
DateSettings dateSettings = new DateSettings(getActivity());
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
DatePickerDialog date_dialog;
date_dialog = new DatePickerDialog(getActivity(), dateSettings, year, month, day);
return date_dialog;
}
}
MainActivity.java
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setDate(View view)
{
PickerDialogs pickerDialogs = new PickerDialogs();
pickerDialogs.show(getSupportFragmentManager(), "date picker?");
}
public void setTime(View view)
{
PickerDialogsTime pickerDialogsTime = new PickerDialogsTime();
pickerDialogsTime.show(getSupportFragmentManager(), "time_picker");
}
}
答案 0 :(得分:0)
您的DialogFragment
没有将选定日期返回给来电者的方法或界面,请参阅以下代码
这里 - 在构造函数内部,我传递了一个OnDateSetListener
对象mDateSetListener
,以便我可以将结果返回给活动
public class DatePickerDialogFragment extends DialogFragment {
private Context context;
private Calendar MinDate, MaxDate;
private OnDateSetListener mDateSetListener;
public DatePickerDialogFragment() {
}
public DatePickerDialogFragment(OnDateSetListener callback, Calendar MinDate, Calendar MaxDate, Context context) {
mDateSetListener = callback;
this.MinDate = MinDate;
this.MaxDate = MaxDate;
this.context = context;
}
public DatePickerDialogFragment(OnDateSetListener callback, Context context) {
mDateSetListener = callback;
this.context = context;
}
DatePickerDialog dd;
DatePicker dp;
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar cal = Calendar.getInstance();
dd = new DatePickerDialog(getActivity(), this.mDateSetListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
dd.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
try {
if (MinDate!=null&&MaxDate!=null) {
((DatePickerDialog) dialog).getDatePicker().setMaxDate(MaxDate.getTimeInMillis());
((DatePickerDialog) dialog).getDatePicker().setMinDate(MinDate.getTimeInMillis());
}
} catch (NullPointerException e) {
dialog.dismiss();
e.printStackTrace();
}
}
});
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return dd;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
当您创建DialogFragmen时,请执行以下操作:
<强>更新强>
在你希望你的datepicker返回值的fragment活动中使用它,注意你将在onDateSet
函数中获取日期值
dialogFragment = new DatePickerDialogFragment(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar cal = GregorianCalendar.getInstance();
cal.set( year, monthOfYear, dayOfMonth);
String currentDateandTime = sdf.format( cal.getTime());
et_date.setText(currentDateandTime);
}
}, context);
这样选择的日期将返回到回调方法并设置为TextView
et_date
在需要时显示
dialogFragment.show(getFragmentManager(), "Date");