在Dialogfragment中显示一个DatePickerDialog

时间:2018-10-14 21:21:49

标签: android android-dialogfragment

我创建了一个具有DialogFragment的{​​{1}},并且当用户按下EditText时,会有一个新的EditText假定从用户那里选择一个日期,然后然后在DatePickerDialog中进行设置。我很难这么做:

  1. 实际上显示了DatePickerDialog,但销毁了旧的DialogFragment。当我单击确定/无论如何时,我将返回到活动。
  2. 我想将选择的日期传递回片段。如果我想将其传递回活动,会更容易,因为我可以使用DialogFragment方法。那么如何将选择的日期传递回称为选择器的片段?

主要片段

getActivity()

DatePickerFragment

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;
}
}

2 个答案:

答案 0 :(得分:1)

您应将'com.wdullaer:materialdatetimepicker:2.3.0'库用于datepickertimepicker对话框。
首先,在app level gradle file中添加依赖项。之后,用NewWorkoutItemFragment extends DialogFragment创建一个新的Java文件。

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);
}


}

和要打开NewWorkoutItemFragment的类,只需使该对话框类的对象像

newWorkDialog= new NewWorkoutItemFragment();
NewWorkoutItemFragment newWorkDialog;
newWorkDialog= NewWorkoutItemFragment.newInstance("T");
newWorkDialog.show(manager,"Show");

在样式文件中只需添加以下行

<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>

然后在res中创建一个新程序包,并将其命名为anim,并在anim文件夹transition_left_sidetransition_right_side中创建两个文件。

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%"/>

和代码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"/>

我想这不会造成任何进一步的错误。

答案 1 :(得分:0)

解决方案根本不涉及datepickerdialog。我的片段中有一个开关箱,用于管理点击事件。我不小心忘记在引起其他事件的事件发生的情况下放一个break;(这是dismiss();之一),这就是为什么datepickerdialog也解散了我的片段。