如何从android TimeSquare库中获取所选日期?

时间:2015-05-13 12:08:58

标签: java android android-view android-calendar android-time-square

如何从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,一旦用户从日历中选择日期。

3 个答案:

答案 0 :(得分:2)

这是我的答案:

 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());
                }
            });

答案 1 :(得分:0)

这是我的屁股

 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) {
                    }
                });

答案 2 :(得分: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)
        {

        }
    });