如何在片段类中创建datePicker和timePicker对话框?

时间:2011-07-12 17:43:57

标签: android

我想知道有没有办法在片段中创建一个datePicker?我正在创建一个常规活动可能,它给我语法错误。这样做的正确方法是什么?

5 个答案:

答案 0 :(得分:22)

扩展Codejoy的答案:

这是我的DatePickerDialogFragment类:

public class DatePickerDialogFragment extends DialogFragment {
    private Fragment mFragment;

    public DatePickerDialogFragment(Fragment callback) {
        mFragment = callback;
    }

     public Dialog onCreateDialog(Bundle savedInstanceState) {
         return new DatePickerDialog(getActivity(), (OnDateSetListener) mFragment, 1980, 7, 16);
     }
}

(请注意,构造函数接受使用此对话框的片段 - 我们将此引用用于DatePickerDialog的回调侦听器字段)

我的片段然后只实现onDateSetListener:

public class SignupFragment extends Fragment implements OnDateSetListener {
...
public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
    // do stuff with the date the user selected
}
}

...然后我从我的片段中显示对话框,如下所示:

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = new DatePickerDialogFragment(this);
newFragment.show(ft, "dialog");

不确定这是否是最好的方法,但似乎工作正常。

答案 1 :(得分:17)

虽然上述答案可能有效,但我相信我有一个更简单的解决方案:

DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 
                             2000, 1,1);
dialog.show();

其中datePickerListerener是这样的:

private DatePickerDialog.OnDateSetListener datePickerListener 
            = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
            //Do whatever you want
        }
};

答案 2 :(得分:5)

以前的anwsers解释得足够好但是我在这里改变了对DialogFragment的onDateSet方法的看法。

您可以将要更改的视图传递给扩展DialogFragment的类的构造函数。然后调用类的setText方法onDateSet方法。这是代码:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public EditText activity_edittext;

public DatePickerFragment(EditText edit_text) {
    activity_edittext = edit_text;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // 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 month, int day) {
        activity_edittext.setText(String.valueOf(month + 1 ) + "/" + String.valueOf(day) + "/" + String.valueOf(year));
    }
}

然后你可以像这样显示你的片段:

public void showDatePickerDialog(View v) {
    new DatePickerFragment((EditText) v).show(getSupportFragmentManager(), "datePicker");
}

设置此方法onClick要更改的EditText方法。像这样:

    <EditText
            android:id="@+id/editTextToShowDate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:focusable="false"
            android:inputType="date"
            android:onClick="showDatePickerDialog" >
   </EditText>

答案 3 :(得分:4)

您很可能需要使用DialogFragment。我在这里找到了一些信息:

Show dialog from fragment?

在这里也是一个很大的帮助:

https://github.com/commonsguy/cw-advandroid/blob/master/Honeycomb/FeedFragments/src/com/commonsware/android/feedfrags/AddFeedDialogFragment.java

这应该可以帮助你顺利开始,我现在正在做这件事。虽然在示例代码中我不使用构建器而是返回:

return new DatePickerDialog(getActivity(), mDateSetListener, mYear, mMonth, mDay);

这似乎有用......虽然我还不知道如何更新调用此DialogFragment的片段上的文本。我认为这样可行,但事实并非如此:

 public void updateDisplay()
 {
     //update our button text from the calling fragment, this isn't quite working
     //doesn't crash just doesn't update...must be missing something.
     View v=getActivity()
        .getLayoutInflater()
        .inflate(R.layout.session_edits, null);
     Button sessionDate = (Button)v.findViewById(R.id.sessionPickDate);
            sessionDate.setText(new StringBuilder()
                            .append(mMonth+1).append("-").append(mDay).append("-").append(mYear).append(" "));

 }

答案 4 :(得分:1)

这是一个想要在片段中使用datepicker的片段:

private Callbacks mCallbacks;
private String tag;

/**
 * A callback interface that all fragments/activities containing this fragment must
 * implement. This mechanism allows activities to be notified of item
 * selections.
 */
public interface Callbacks {
    /**
     * Callback for when an item has been selected.
     */
    public void onSelected(Date date, String tag);
}





  public static DatePickerFragment newInstance(Fragment fragment, 
                                               Date date, String tag)         {
    DatePickerFragment dateFragment = new DatePickerFragment();
    Calendar c = Calendar.getInstance();
    if (date != null) {
        c.setTime(date);
    }
    dateFragment.setYear(c.get(Calendar.YEAR));
    dateFragment.setMonth(c.get(Calendar.MONTH));
    dateFragment.setDay(c.get(Calendar.DAY_OF_MONTH));
    dateFragment.setmCallbacks((Callbacks) fragment);
    dateFragment.setTag(tag);
    return dateFragment;
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, day);
    date = new Date(cal.getTime().getTime());
    setType(1);
    mCallbacks.onSelected(date,tag);
}

并在你的片段中:

   public class DetailFragment extends Fragment implements
    DatePickerFragment.Callbacks {

。 。

    private void startDatePicker(String tag) {

    DialogFragment newFragment = DatePickerFragment.newInstance(this,
            invoiceModell.getInvoice().getInvoiceDate(), tag);
    newFragment.show(getActivity().getSupportFragmentManager(),
            "datePicker");
}


    @Override
public void onSelected(Date date, String tag) {
    if (tag.equals(FIRSTDATE))
        invoiceModell.getInvoice().setInvoiceDate(date);
    else if (tag.equals(SECONDDATE))
        invoiceModell.getInvoice().setDueDate(date);
}