如何在设置文本日期更改后调用API

时间:2017-10-11 05:24:54

标签: java android datepicker calendar

我希望在日期文本发生变化时调用API。当活动打开时,我设置了一些初始日期,从日期到日期。如果用户更改了那些日期,我想调用一个API。请帮忙。

private void setListener() {


    Calendar calendar = Calendar.getInstance();
    datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            StringBuilder  selectedFromDate = new StringBuilder().append(dayOfMonth).append("-").append(monthOfYear + 1).append("-").append(year);
           TextFromDate.setText(selectedFromDate);

        }
    },


    calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
          TextFromDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            datePickerDialog.getDatePicker().setMaxDate(new Date().getTime());
            datePickerDialog.show();

        }
    });
}

1 个答案:

答案 0 :(得分:0)

这很简单:

private void setListener() {


Calendar calendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        StringBuilder  selectedFromDate = new StringBuilder().append(dayOfMonth).append("-").append(monthOfYear + 1).append("-").append(year);
       TextFromDate.setText(selectedFromDate);
       //***Call your server call here. As this method is called when you select a date.***
    }
},


        calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
      TextFromDate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        datePickerDialog.getDatePicker().setMaxDate(new Date().getTime());
        datePickerDialog.show();



    }
});
}

希望这有帮助。