2013-11-01 37 views
1

我已经实现了如下的日期选择器。如何在片段中实现Android日期选择器

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

Thanx提前。

+0

检查此问题http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment – Raghunandan

回答

1

使用此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)在你的活动或片段实施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"); 

&呼叫的DatePicker从活动(或FragmentActivity)像这样

   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" u可以使用TIME_PICKER(时间选择器对话框)& DIALOG(对于简单对话框) 按发送"dialog_id"根据发送数据通过绑定到CustomDialogFragment

+0

Thanx很多。有效。 – Rose18

+0

那么我应该如何从日期选择器中获取日期并将其设置在文本视图中。可以解释吗? – Rose18

+0

为u必须执行OnDateSetListener上实现的方法后,你的活动或片段,它会回到结果onDateSet(DatePicker的观点,年整型,诠释monthOfYear, \t \t \t INT请将dayOfMonth)方法.. –

相关问题