1

我已经实现了一个日期选择器,如下所示。

private void updateDisplay() {
    String str_date = "";
    if ((Month + 1) >= 10) {
        if (Day < 10) {
            str_date = Year + "-" + (Month + 1) + "-0" + Day;
        } else {
            str_date = Year + "-" + (Month + 1) + "-" + Day;
        }
    } else {
        if (Day < 10) {
            str_date = Year + "-0" + (Month + 1) + "-0" + Day;
        } else {
            str_date = Year + "-0" + (Month + 1) + "-" + Day;
        }

    }
    date.setText("" + str_date);
}

protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(Year, Month, Day);
        break;
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(viewLoad.getContext(),
                mDateSetListener, Year, Month, Day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        Year = year;
        Month = monthOfYear;
        Day = dayOfMonth;
        updateDisplay();
    }
};

这是我的按钮代码段。

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btnDate:
        showDialog(DATE_DIALOG_ID);
        break;

这工作正常。现在的问题是我需要在片段中实现一个日期选择器。任何人都可以解释如何做到这一点。

编辑代码

 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        Year = year;
        Month = monthOfYear;
        Day = dayOfMonth;
        updateDisplay();
    }
};

提前谢谢。

4

2 回答 2

2

使用这个包含使用的CustomDialogFragment

  1. TimePicker 对话框片段
  2. DatePicker 对话框片段
  3. 简单对话框片段

    CustomeDialogFragment 类

公共类 CustomDialogFragment 扩展 DialogFragment {

            public static final int DATE_PICKER = 1;
            public static final int TIME_PICKER = 2;
            public static final int DIALOG = 3;

            Context mContext;
            String mMessage;
            boolean finishActivity;

           // private Fragment mCurrentFragment;
            Activity mActivity;

            /**
             *  this constructor is used for datepicker & Timepicker
             */
            public CustomDialogFragment (Fragment fragment ) {
            //        mCurrentFragment = fragment;
            }

            public CustomDialogFragment (Activity mActivity ) {
                this.mActivity = mActivity;
        }

            /**
             *  this constructor is used for simple dialog fragment
             */
            public CustomDialogFragment(Context context, String message, final boolean finishActivity) {
                mContext = context;
                mMessage = message;
                this.finishActivity = finishActivity;
            }



            public Dialog onCreateDialog(Bundle savedInstanceState) {
                Bundle bundle = new Bundle();
                bundle = getArguments();
                int id = bundle.getInt("dialog_id");
                switch (id) {
                case DATE_PICKER:
                    return new DatePickerDialog(getActivity(),
                            (OnDateSetListener)mActivity, bundle.getInt("year"),
                            bundle.getInt("month"), bundle.getInt("day"));

               case TIME_PICKER:
                    return new TimePickerDialog(getActivity(),
                            (OnTimeSetListener)mActivity,bundle.getInt("hour"),
                            bundle.getInt("minute"), true);

               case DIALOG:
                   AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
                   alertDialogBuilder.setTitle(R.string.app_name);
                   alertDialogBuilder.setMessage(mMessage);
                   alertDialogBuilder.setIcon(R.drawable.ic_launcher);
                   alertDialogBuilder.setCancelable(false);
                   alertDialogBuilder.setPositiveButton(
                            getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {

                       @Override
                       public void onClick(DialogInterface dialog, int arg1) {

                           dialog.dismiss();
                           if (finishActivity == true) {
                               Activity act = (Activity) mContext;
                               act.finish();
                           }

                       }
                   });
                   alertDialogBuilder.setNegativeButton(getResources().getString(R.string.cancel_btn_title), new DialogInterface.OnClickListener() {

                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           CustomDialogFragment.this.dismiss();
                       }
                   });


                   return alertDialogBuilder.create();

                }

               //Define your custom dialog or alert dialog here and return it.
               return new Dialog(getActivity());
            }
        }

并以这种方式使用日期选择器

1)在您的 Activity 或片段中实现OnDateSetListener(完成后它将返回结果给onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 方法)

2)像这样从片段调用DatePicker

   CustomDialogFragment dialog = new CustomDialogFragment(YourFragment.this);
    Bundle bundle = new Bundle();
    bundle.putInt("dialog_id", CustomDialogFragment.DATE_PICKER);
    bundle.putInt("year", mYear);
    bundle.putInt("month", mMonth);
    bundle.putInt("day", mDay);
    dialog.setArguments(bundle);
    dialog.show(getFragmentManager(), "dialog"); 

& 像这样从 Activity(或 FragmentActivity)调用 DatePicker

               CustomDialogFragment dialog = new CustomDialogFragment(this);
                Bundle bundle = new Bundle();
                bundle.putInt("dialog_id", CustomDialogFragment.DATE_PICKER);
                bundle.putInt("year", mYear);
                bundle.putInt("month", mMonth);
                bundle.putInt("day", mDay);
                dialog.setArguments(bundle);
                dialog.setCancelable(false);
                dialog.show(this.getSupportFragmentManager(), "dialog");

&按照设置,"dialog_id"您可以使用TIME_PICKER(用于时间选择器对话框)&DIALOG(用于简单对话框)"dialog_id"根据通过捆绑发送数据发送到CustomDialogFragment

于 2013-11-01T03:52:51.203 回答
0

据我所知,不能showDialog()在片段中使用。它也已被弃用。使用DialogFragment. 根据需要从本教程中学习 http://www.kylebeal.com/2011/11/android-datepickerdialog-and-the-dialogfragment/

于 2013-11-01T03:42:23.083 回答