问题
I created a DialogFragment
that has an EditText
, and when the user presses the EditText
there is a new DatePickerDialog
that suppose to pick a date from the user and then set it in the DialogFragment
. I am having a hard time to do so:
- The DatePickerDialog actually shows up but destroys the old
DialogFragment
. When I click ok/whatever I return to the activity. - I want to pass the date that I picked back to the fragment. It was easier if I wanted to pass it back to an activity because I can use
getActivity()
method. So how can I pass the date I picked back to the fragment that called the picker?
Main Fragment
public class NewWorkoutItemFragment extends android.app.DialogFragment implements
View.OnClickListener {
private String mDate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_new_workout_item, container, false);
//A lot of code
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.NewWorkout_date_et:
//Call to DatePickerDialog
DialogFragment pickDate = new DatePickerFragment();
pickDate.show(getFragmentManager(), "show");
}
public void setmDate(String mDate) {
this.mDate = mDate;
}
}
DatePickerFragment
public class DatePickerFragment extends android.app.DialogFragment implements DatePickerDialog.OnDateSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker.
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it.
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String dateString = Integer.toString(dayOfMonth) +
"/" + Integer.toString(month+1) +
"/" + Integer.toString(year);
//note- Return dateString back to NewWorkoutItemFragment, using setmDate
}
}
回答1:
You should use 'com.wdullaer:materialdatetimepicker:2.3.0'
library for datepicker
or timepicker
dialogs.
First of all add the dependency in app level gradle file
. After that create a new java file with NewWorkoutItemFragment extends DialogFragment
.
public class NewWorkoutItemFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@OnClick(R.id.layout_time)
public void onClickTime(View view){
calendar= Calendar.getInstance();
datePickerDialog= TimePickerDialog.newInstance(NewWorkoutItemFragment.this,
calendar.get(Calendar.YEAR), // Initial year selection
calendar.get(Calendar.MONTH), // Initial month selection
calendar.get(Calendar.DAY_OF_MONTH) // Inital day selection
false);
datePickerDialog.setThemeDark(false);
datePickerDialog.setAccentColor(Color.parseColor("#58a5f0"));
datePickerDialog.setTitle("Select Date");
datePickerDialog.show(getFragmentManager(),"DatePicker Dialog");
}
public NewWorkoutItemFragment() {
}
public static NewWorkoutItemFragment newInstance (String title) {
NewWorkoutItemFragment frag = new NewWorkoutItemFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}
@Override
public void onAttach(final Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
final Window window = dialog.getWindow();
if (window != null) {
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
windowLayoutParams.dimAmount = DIM_AMOUNT;
}
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater,
final ViewGroup container,
final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.dlg_network, container, false);
return view;
}
@Override
public void onResume() {
super.onResume();
final int width = getScreenWidth();
changeWindowSizes(width, ViewGroup.LayoutParams.WRAP_CONTENT);
}
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
private void changeWindowSizes(int width, int height) {
final Window window = getDialog().getWindow();
if (window != null) {
// hack to change window width
window.setLayout(width, height);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
mUnbinder.unbind();
}
@Override
public int getTheme() {
return R.style.CustomDialog;
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year;
dateTextView.setText(date);
}
}
and the class where you want to open NewWorkoutItemFragment
just make the object of this dialog class like
newWorkDialog= new NewWorkoutItemFragment();
NewWorkoutItemFragment newWorkDialog;
newWorkDialog= NewWorkoutItemFragment.newInstance("T");
newWorkDialog.show(manager,"Show");
in styles file just add the following lines
<style name="CustomDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowAnimationStyle">@style/CustomDialogAnimation</item>
</style>
<style name="CustomDialogAnimation">
<item name="android:windowEnterAnimation">@anim/translate_left_side</item>
<item name="android:windowExitAnimation">@anim/translate_right_side</item>
</style>
And create a new package inside res
and name it anim
and create two files in anim
folder transition_left_side
and transition_right_side
.
Code for transition_left_side
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="600"
android:fromXDelta="100%"
android:toXDelta="0%"/>
and code for transition_right_side
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="600"/>
I guess this will not create any further errors.
回答2:
The solution isn't about the datepickerdialog
at all. I had a switch-case in my fragment that manages the events of clicks. I accidentally forgot to put a break;
after the case which caused a flow to the other events (one of them was dismiss();
), and that's why the datepickerdialog also dismissed my fragment.
来源:https://stackoverflow.com/questions/52807210/showing-a-datepickerdialog-inside-a-dialogfragment