【问题标题】:Android Datepicker Fragment - How to do something when the user sets a date?Android Datepicker Fragment - 用户设置日期时如何做某事?
【发布时间】:2012-07-30 01:53:09
【问题描述】:

我在这里学习教程: http://developer.android.com/guide/topics/ui/controls/pickers.html

我得到了一些工作,日期选择器出现了。但是,我不知道如何在用户设置日期时采取行动。

我看到他们有这个方法:

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
}

但如果我在其中放入任何代码,那将无济于事。

我对 Java 不太熟悉,所以我不确定需要哪些额外代码才能使用所选日期更新文本视图。

我确实添加了这段代码:

private DatePickerDialog.OnDateSetListener mDateListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int month, int day) {
                updateDate(year, month, day);
            }
        };

public void updateDate(int year, int month, int day) {
    TextView bdate = (TextView)findViewById(R.id.birthDate);
    Date chosenDate = new Date(year, month, day);
    java.text.DateFormat dateFormat = DateFormat.getDateFormat(getApplicationContext());
    bdate.setText(dateFormat.format(chosenDate));
}

但这无济于事。

我确实发现了这个几乎完全一样的问题: Get date from datepicker using dialogfragment

但是,在该解决方案中,不会使用 (EditSessionActivity) 将 DatePickerFragment 类限制为仅使用 EditSessionActivity?

【问题讨论】:

    标签: android datepicker android-fragments


    【解决方案1】:

    您应该使用静态工厂将参数传递给 DialogFragment。来自docs

    "每个fragment都必须有一个空的构造函数,这样才能在恢复activity的状态时实例化。强烈建议子类不要有其他带参数的构造函数,因为fragment重新被调用时这些构造函数不会被调用实例化;相反,调用者可以使用 setArguments(Bundle) 提供参数,然后由 Fragment 使用 getArguments() 检索。"

    同样,Fragment 子类也应该是静态的,这样它就可以在不依赖父活动或 Fragment 的情况下重新实例化自己。如果您不遵守这些准则,您有时会在 Fragment 尝试恢复时遇到崩溃或错误。

    Android 的docs 推荐的构建 DialogFragment 的方法是使用静态工厂方法。下面是一个接收初始日期和 OnDateChangedListener 的日期选择器片段示例:

    public static class DatePickerFragment extends DialogFragment{
    
        private OnDateSetListener onDateSetListener;
    
        static DatePickerFragment newInstance(Date date, OnDateSetListener onDateSetListener) {
            DatePickerFragment pickerFragment = new DatePickerFragment();
            pickerFragment.setOnDateSetListener(onDateSetListener);
    
            //Pass the date in a bundle.
            Bundle bundle = new Bundle();
            bundle.putSerializable(MOVE_IN_DATE_KEY, date);
            pickerFragment.setArguments(bundle);
            return pickerFragment;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            super.onCreateDialog(savedInstanceState);
    
            Date initialDate = (Date) getArguments().getSerializable(MOVE_IN_DATE_KEY);
            int[] yearMonthDay = ymdTripleFor(initialDate);
            DatePickerDialog dialog = new DatePickerDialog(getActivity(), onDateSetListener, yearMonthDay[0], yearMonthDay[1],
                    yearMonthDay[2]);
            return dialog;
        }
    
        private void setOnDateSetListener(OnDateSetListener listener) {
            this.onDateSetListener = listener;
        }
    
        private int[] ymdTripleFor(Date date) {
            Calendar cal = Calendar.getInstance(Locale.US);
            cal.setTime(date);
            return new int[]{cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)};
        }
    }
    

    然后您可以按如下方式创建这些对话框片段之一:

    DatePickerFragment.newInstance(yourDate, yourOnDateSetListener);
    

    【讨论】:

    • 也许你应该补充一下,如果你先实例化片段然后使用它的'show()`方法让它出现在屏幕上,就会弹出对话框。
    【解决方案2】:

    我相信这里可以使用 DialogFragments:

    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
    

    定义 DatePickerFragment 并实现下面的方法

    public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    DateEdit.setText(day + "/" + (month + 1) + "/" + year);
    }
    

    取自Android DialogFragment Example

    【讨论】:

      【解决方案3】:

      根据我的经验,这段代码可以完美运行!,我使用的是 win10 Android Studio 2.2...

      import android.app.Dialog;
      import android.support.v4.app.DialogFragment;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.app.DatePickerDialog;
      import android.widget.DatePicker;
      import android.widget.EditText;
      import java.util.Calendar;
      
      
      public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);}
      
          public void datePicker(View view){
              DatePickerFragment fragment_date = new DatePickerFragment();
              fragment_date.show(getSupportFragmentManager(),"datePicker");}
      
          @Override
          public void onDateSet(DatePicker view, int year, int month, int day) {
               String day_x = ""+ day  ,month_x = ""+(month+1);
               if (day   < 9 ){ day_x   = "0" + day_x;}
               if (month < 9 ){ month_x = "0" + month_x;}
               ((EditText) findViewById(R.id.txtDate)).setText(day_x + "/" + month_x + "/" + year);}
      
          public static class DatePickerFragment extends DialogFragment {
              @Override
              public Dialog onCreateDialog (Bundle savedInstanceState){
                  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);
                  return new DatePickerDialog(getActivity() ,(DatePickerDialog.OnDateSetListener)this,day,month,year);}}
      
      }
      

      我有一个“textView txtDate”和一个“带有 onclick = datePicker 的按钮”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-01
        • 1970-01-01
        • 2021-03-22
        • 1970-01-01
        • 2014-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多