片段内的DatePicker

时间:2015-10-08 09:44:24

标签: android android-fragments datepicker onclick onclicklistener

嗨,我是Android开发的新手。我遇到了问题。 我有一个片段。在Fragment视图中,我获得了datepicker的Image按钮。运行代码时没有发现错误。但是当我点击图像按钮时,“日期选择器”对话框没有显示..

任何人都可以帮助我。

这是代码:

RecyclerView

2 个答案:

答案 0 :(得分:0)

尝试这样的希望工作

private void displayDate() {

    Calendar mCalendar = Calendar.getInstance();
    new DatePickerDialog(getActivity(), (OnDateSetListener) new OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
    // TODO Auto-generated method stub
       PickDate.append(monthOfYear + 1).append("-").append(
           .append("-").append(year));

    }
    }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
    mCalendar.get(Calendar.DATE));
    }

答案 1 :(得分:0)

在您的按钮中,点击这样的DateFragment

yourButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        DialogFragment newFragment = new DateFragment();
        newFragment.show(getFragmentManager(), "DatePicker");

    }
});

for DateFragment

public class DateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    int yy = calendar.get(Calendar.YEAR);
    int mm = calendar.get(Calendar.MONTH);
    int dd = calendar.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm+1, dd);
    }
    public void populateSetDate(int year, int month, int day) {
        dob.setText(month+"/"+day+"/"+year);
        }

}