如何选择我想在日期选择器中放置日期的EditText小部件?

时间:2015-11-23 04:33:08

标签: android android-fragments android-datepicker

我有一个项目,其中有两个带按钮的自定义framelayout edittexts,它们是打开的DatePickerFragmentDialog。 我需要在这两个edittext小部件之间进行选择,我想从datepicker对话框中选择日期。

这是我创建Fragment

的代码
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.butSince:
            DialogFragment newFragment = new DatePickerFragment();
            new ShowDate().setEditText(etSince);
            newFragment.show(getFragmentManager(), "datePicker");

            break;
    }
}

这是我的DatePickerFragment

public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

String date;
EditText editText;
ShowDate showDate;

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

    editText = new ShowDate().getEditText();
    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

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


}
}

它是一个“选择方法”代码:

import android.widget.EditText;

public class ShowDate {
EditText editText;

public EditText getEditText() {
    return editText;
}

public void setEditText(EditText editText) {
    this.editText = editText;
}
}

我在NullPointerException中有onDateSet

0 个答案:

没有答案