如何在Fragment中覆盖onCreateDialog方法

时间:2016-11-12 05:26:26

标签: android android-fragments

我无法覆盖onCreateDialog()中的Fragment方法。但它允许我覆盖Activity

@Override
protected Dialog onCreateDialog(int id) {
    if (id == 999) {
        return new DatePickerDialog(this, myDateListener, year, month, day);
    }
    return null;
}

2 个答案:

答案 0 :(得分:5)

Use DialogFragment .In your fragment to extend DialogFragment class

public class Fragment extends DialogFragment {

  @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {

}
}

答案 1 :(得分:1)

There is another method if you want to use the date and time picker dialog . use https://github.com/wdullaer/MaterialDateTimePicker it is the material Dialog for date and time picker. you can use it in both activity and Fragment . for farment you can use if as

DatePickerDialog mDatepickerDialog = DatePickerDialog.newInstance(this,
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH)
    );

if any problem or issue you can write in comments