Android Datepicker Fragment - 用户设置日期时如何执行某些操作? [英] Android Datepicker Fragment - How to do something when the user sets a date?

查看:175
本文介绍了Android Datepicker Fragment - 用户设置日期时如何执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以下教程:
http: //developer.android.com/guide/topics/ui/controls/pickers.html



我有事情工作,并显示datepicker。但是,当用户设置日期时,我被困在如何采取行动。



我看到他们有这样的方法:

  public void onDateSet(DatePicker view,int year,int month,int day){
//执行某些与用户选择的日期
}

但是,如果我把任何代码放在其中,则不会执行任何操作。



我不熟悉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 selectedDate = new Date(年,月,日);
java.text.DateFormat dateFormat = DateFormat.getDateFormat(getApplicationContext());
bdate.setText(dateFormat.format(selectedDate));
}

但这不做任何事。



找到这个几乎确切的问题:
从datepicker获取日期使用对话框片段



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

解决方案

您应该使用静态工厂将参数传递给DialogFragment。从文档



每个片段都必须有一个空的构造函数,所以在恢复其活动状态时可以实例化,强烈建议子类没有其他具有参数的构造函数,因为这些构造函数在片段是重新实例化;相反,参数可以由调用者使用setArguments(Bundle)提供,稍后由片段使用getArguments()检索。



同样,Fragment子类应该是静态的,因此它可以重新实例化自身而不依赖于父活动或片段。如果您不遵守这些准则,当您的片段尝试恢复时,您有时会遇到崩溃或错误。



从Android的 docs ,是使用静态工厂方法。这是一个采用初始日期的日期选择器片段和OnDateChangedListener的示例:

  public static class DatePickerFragment extends DialogFragment {

private OnDateSetListener onDateSetListener;

static DatePickerFragment newInstance(Date Date,OnDateSetListener onDateSetListener){
DatePickerFragment pickerFragment = new DatePickerFragment();
pickerFragment.setOnDateSetListener(onDateSetListener);

//将日期传递给捆绑包。
Bundle bundle = new Bundle();
bund.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]);
返回对话框;
}

private void setOnDateSetListener(OnDateSetListener listener){
this.onDateSetListener = listener;
}

private int [] ymdTripleFor(Date date){
日历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); 


I'm following the tutorial here: http://developer.android.com/guide/topics/ui/controls/pickers.html

I got things to work and the datepicker shows up. However, I am stuck on how to act on when the user sets the date.

I see they have this method:

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

But that does nothing if I put any code in it.

I'm not that familiar with Java so I'm not sure what additional code is needed in order to update a textview with the chosen date.

I did add this code:

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

But that does nothing.

I did find this almost exact question: Get date from datepicker using dialogfragment

However in that solution wouldn't using (EditSessionActivity) limit the DatePickerFragment class to working with only the EditSessionActivity?

解决方案

You should use a static factory to pass arguments to the DialogFragment. From the docs,

"Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments()."

Similarly, the Fragment subclass should be static, so that it can re-instantiate itself without relying on the parent activity or Fragment. If you don't adhere to these guidelines, you will sometimes get crashes or errors when your Fragment tries resume.

The recommended way to build a DialogFragment, from Android's docs, is to use a static factory method. Here's an example of a date picker fragment that takes in an initial date and an 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)};
    }
}

You can then create one of these dialog fragments as follows:

DatePickerFragment.newInstance(yourDate, yourOnDateSetListener);

这篇关于Android Datepicker Fragment - 用户设置日期时如何执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆