如何在edittext android对话框中设置日期?

时间:2016-12-04 11:19:12

标签: android datepicker dialog android-edittext fragment

我有一个datepicker对话框放入edittext设置生日..设置日期后的对话框返回当前日期..我需要的是显示我选择的日期选择它后的datepicker对话框?

Datepicker对话框类:

    public class DatePickerDialog2 extends DialogFragment{

public static final String ARG_DEFAULT = "default";

private DatePicker datePicker;
private String defaultDate;

    private DatePickerDialog.OnDateSetListener onDateListener;
public DatePickerDialog2() {
    try {
        OnDatePickerDoneButtonClickedListener act = (OnDatePickerDoneButtonClickedListener) getActivity();

    } catch (ClassCastException e) {
        throw new ClassCastException("Must implement OnDatePickerDoneButtonClickedListener");
    }
}


    public interface OnDatePickerDoneButtonClickedListener {
    void OnDatePickerDoneButtonClicked(String date);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    defaultDate = getArguments().getString(ARG_DEFAULT);

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     Calendar calendar = Calendar.getInstance();
    if (TextUtils.isEmpty(defaultDate)) {
        DateFormat df = new SimpleDateFormat("dd/MMM/yyyy");;
        try {
            calendar.setTime(df.parse(new Date().toString()));
            Log.d("first", defaultDate + " -");
        } catch (ParseException e) {
            e.printStackTrace();
        }

    } else {
        try {
            DateFormat df =  new SimpleDateFormat("dd/MMM/yyyy");
            calendar.setTime(df.parse(defaultDate));
            Log.d("second", defaultDate + " -");
        } catch (ParseException e1) {
            Log.d("third",defaultDate+" -");
        }
    }
    datePicker = new DatePicker(getActivity());

    datePicker.setCalendarViewShown(false);
    datePicker.setMaxDate(new Date().getTime()-1 * 24 * 3600 * 1000);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

//            String date =datePicker.getYear()+ "-"+ datePicker.getDayOfMonth() + "-" + (datePicker.getMonth() + 1)   ;
            int day = datePicker.getDayOfMonth();
            int month = datePicker.getMonth();
            int year =  datePicker.getYear();

         final Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, day);
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy");
            String date = sdf.format(calendar.getTime());
            OnDatePickerDoneButtonClickedListener act = (OnDatePickerDoneButtonClickedListener) getActivity();
            act.OnDatePickerDoneButtonClicked(date);
            dismiss();
        }
    })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dismiss();
                }
            })
                    //.setTitle("Select date")
            .setView(datePicker);
    // Create the AlertDialog object and return it
    return builder.create();

}
}

edittext我称该对话框打开日期选择器:

  birthday.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View v) {
                                   DatePickerDialog2 dialog = new DatePickerDialog2();
                                   Bundle bundle = new Bundle();
                                   bundle.putString("dateAsText", birthday.getText().toString());
                                   dialog.setArguments(bundle);
                                   dialog.show(getSupportFragmentManager(), "date_picker_frag");
                               }
                           });

1 个答案:

答案 0 :(得分:0)

试试这个

  template:
    '<div>'+
      'Form with id = {{id}}'+
      '<br/>'+
      'has title = {{item.details.Title}}'+
    '</div>',
  data: function(){
    return {
      id: '',
      item: {}
    }
  },
  created: function() {
    this.get()
  },  
  methods: {
    get: function() {
      var self = this
      id = window.location.hash
      id = id.replace('#/whatever/','')
      axiosInstance.get('/thebackendresource/'+id) // <--- make http calls etc
        .then(function (response) {
          self.id = id
          self.item = response.data
          console.log(self.item)
        }).catch(function (error) {
          console.log(error)
        }
      );
    }
  }