3

如何从 android TimeSquare 库中获取用户选择的日期?我使用了 getselectedate() 但它不起作用,而且 OnClickListener 也无法使用它。

 public class HomeActivity extends Activity {

    private String dateHolder; 
    private CalendarPickerView calenderView; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);


        calenderView = (CalendarPickerView) findViewById(R.id.calendar_view);
        calenderView.init(lastYear.getTime(), nextYear.getTime()) 
                .inMode(SelectionMode.SINGLE) 
                .withSelectedDate(new Date());
        TextView tv = (TextView)findViewById(R.id.calendar_view);
        tv.setText("Pickup Date:" + **HOW_TO_PRINT_SELECTED_DATE_HERE** );    
    }

}

在上面的代码示例中,我必须在 TextView tv (HOW_TO_PRINT_SELECTED_DATE_HERE) 中显示 PickUpDate,只要用户从日历中选择日期。

4

3 回答 3

3

这是我的回答:

 calenderView.setOnDateSelectedListener(new OnDateSelectedListener() {

            @Override
            public void onDateUnselected(Date date) {

            }

            @Override
            public void onDateSelected(Date date) {

            Toast.makeText(HomeActivity.this,calenderView.getSelectedDate().getTime()+"", Toast.LENGTH_LONG).show();            

            tv.setText("Pickup Date:" +calenderView.getSelectedDate().getTime());
                }
            });
于 2015-05-17T04:41:00.470 回答
1

这是我的回答

 CalendarPickerView datePicker;

    Date today = new Date();
            Calendar nextYear = Calendar.getInstance();
            nextYear.add(Calendar.YEAR, 1);
        datePicker = findViewById(R.id.calendar);
                datePicker.init(today, nextYear.getTime())
                        .inMode(CalendarPickerView.SelectionMode.RANGE)
                        .withSelectedDate(today);

                datePicker.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
                    @Override
                    public void onDateSelected(Date date) {
                        //String selectedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);

                        Calendar calSelected = Calendar.getInstance();
                        calSelected.setTime(date);


                        String selectedDate = "" + calSelected.get(Calendar.DAY_OF_MONTH)
                                + "-" + (calSelected.get(Calendar.MONTH) + 1)
                                + "-" + calSelected.get(Calendar.YEAR);

                        Toast.makeText(timesquare.this, selectedDate, Toast.LENGTH_SHORT).show();
                          dialog(selectedDate);
                    }

                    @Override
                    public void onDateUnselected(Date date) {
                    }
                });
于 2019-02-23T07:46:30.393 回答
0
calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener()
    {
        @Override
        public void onDateSelected(Date date)
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy", Locale.ENGLISH);
            tv.setText("Pickup Date:" +dateFormat.format(date));
        }
        @Override
        public void onDateUnselected(Date date)
        {

        }
    });
于 2020-03-05T10:17:36.617 回答