【问题标题】:Android Datepicker - How to pass date from activity to fragmentAndroid Datepicker - 如何将日期从活动传递到片段
【发布时间】:2013-05-16 22:35:37
【问题描述】:

所有文档和示例似乎都与“如何从活动中的日期选择器对话框中选择日期”有关。几乎所有这些都将当前日期设置为日期选择器对话框中的默认日期。但是如何传递一个特定的日期,以便当日期选择器打开时,它显示该日期而不是当前日期?

我面临的问题是,当用户单击按钮并且日期选择器对话框第一次打开时,它会显示当前时间。

用户更改值并点击完成。

再次单击该按钮时,将打开日期选择器对话框。但是,该值仍然是默认日期,而不是更改的日期!

代码:

public class DatePickerFragment extends SherlockDialogFragment
        implements android.app.DatePickerDialog.OnDateSetListener{
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //TODO Use current date is date null, else show currently displayed date
        
        // 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 monthOfYear,
            int dayOfMonth) {
        ((NewTransactionActivity) getActivity()).updateDate(year, monthOfYear, dayOfMonth);
        
    }

}

上面的代码是日期选择器总是显示默认日期的原因。但是如何将日期值传递给这个片段?

这是来自活动的代码:

public void showDatePickerDialog(View v) {
    Bundle currentDate = new Bundle();
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

【问题讨论】:

    标签: android android-activity datepicker


    【解决方案1】:
           //Get the date on the button now
            Date buttonDate = SimpleDateFormat.getDateInstance().parse(mDate.getText().toString());
    
            //Create a bundle to pass the date
            Bundle currentDate = new Bundle();
            currentDate.putLong("setDate", buttonDate.getTime());
    
            //Pass the bundle to the fragment
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.setArguments(currentDate);
            newFragment.show(getSupportFragmentManager(), "datePicker");
    

    现在在片段中这样做:

        //Read the passed bundle from the activity
    Bundle setDate = this.getArguments();
    Long currDate = setDate.getLong("setDate");
    
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    c.setTimeInMillis(currDate);
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    

    【讨论】:

      【解决方案2】:

      你需要两件事:

      1) 传递一个输入参数(或参数给DataPickerFragment)..

      Bundle args = new Bundle();
      args.putInt("someInt", someInt);
      args.putString("someString", someString);
      // Put any other arguments
      myFragment.setArguments(args);
      

      2) 设置对话日期(请注意月份从 0 开始,到 11 结束)

      这是一个使用对话框完成的示例

      private void showEditDatePopup()
      {
          String dateString = mBirthDate.getText().toString();
          java.util.Date inputDate  = null;
          Calendar newCalendar = Calendar.getInstance();
          if (dateString.length() > 0)
          {
              try
              {
                  inputDate = DateFormat.getDateInstance(DateFormat.MEDIUM).parse(dateString);
                  newCalendar = Calendar.getInstance();
                  newCalendar.setTime(inputDate);
              } catch (Exception e)
              {
                  newCalendar = Calendar.getInstance();
                  newCalendar.setTime(inputDate);
              }
          }
      
      
          DatePickerDialog dateDialog = new DatePickerDialog(this, new OnDateSetListener() {
      
              public void onDateSet(DatePicker view, int year, int monthOfYear,
                      int dayOfMonth) {
                  Calendar newDate = Calendar.getInstance();
                  newDate.set(year, monthOfYear, dayOfMonth);
                  mBirthDate.setText(DateFormat.getDateInstance(DateFormat.MEDIUM).format(newDate.getTime()));
              }
      
          },newCalendar.get(Calendar.YEAR)
           ,newCalendar.get(Calendar.MONTH)
           ,newCalendar.get(Calendar.DAY_OF_MONTH));
      
          dateDialog.show();
      }
      

      【讨论】:

      • 我开发的不同之处在于日期触发器在一个活动中,而日期选择器在一个片段中。所以你的回答为我提供了正确的提示和方向。谢谢
      【解决方案3】:

      这对我来说很好用

      在 MainActivity 中:

      Calendar yourDate = Calendar.getInstance();
          
       public void showDatePickerDialog(View v) {
                   DialogFragment newFragment = new DatePickerFragment();
                   Bundle args = new Bundle();
                   args.putInt("year", yourDate.get(Calendar.YEAR));
                   args.putInt("month", yourDate.get(Calendar.MONTH));
                   args.putInt("day",yourDate.get(Calendar.DAY_OF_MONTH));
                   newFragment.setArguments(args);
                   newFragment.show(getSupportFragmentManager(), "datePicker");
               }
      

      在 DatePickerFragment 中:

       @Override
          public Dialog onCreateDialog(Bundle savedInstanceState) {
              // Use the current date as the default date in the picker
              Bundle bundle = this.getArguments();
              int year = bundle.getInt ("year");
              int month = bundle.getInt("month");
              int day = bundle.getInt("day");
              listener = (OnFragmentInteractionListener) getActivity();
              // Create a new instance of DatePickerDialog and return it
              return new DatePickerDialog(getActivity(), this, year, month, day);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 2018-03-26
        相关资源
        最近更新 更多