Android的日期选择器碎片 - 如何做一些事情,当用户设置一个日期? [英] Android Datepicker Fragment - How to do something when the user sets a date?

查看:262
本文介绍了Android的日期选择器碎片 - 如何做一些事情,当用户设置一个日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的教程在这里: <一href="http://developer.android.com/guide/topics/ui/controls/pickers.html">http://developer.android.com/guide/topics/ui/controls/pickers.html

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
}

但是,这并没有什么,如果我把所有code在里面。

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

我没那么熟悉Java,所以我不知道什么额外的code是必要的,以更新与选定的日期一个TextView。

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.

我没有添加此code:

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.

我发现这几乎是确切的问题: 从日期选择器采用获取日期dialogfragment

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

然而,在这种解决办法将不会使用(EditSessionActivity)限制DatePickerFragment类只与EditSessionActivity工作?

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

推荐答案

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

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

每一个片段都必须有一个空的构造函数,所以它可以在恢复其活性的状态被实例化。强烈建议子类不具有其他构造带参数,因为这些构造函数将不会被调用时的片段重实例化;相反,参数可以被调用者与setArguments(束)和再由片段,getArguments()提供的

"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.

推荐的方式来建立一个DialogFragment,从Android的文档,是使用静态工厂方法。这里有一个日期选择器片段需要在初始日期和OnDateChangedListener的例子​​:

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的日期选择器碎片 - 如何做一些事情,当用户设置一个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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