弹出日历视图

时间:2014-12-09 07:17:32

标签: android android-layout android-activity calendar

我在弹出窗口中使用日历视图来获取日期并在textview中设置它。 但onSelectedDayChange不工作。我改变日期时,Toast不工作​​。

currentDate.setOnClickListener(new OnClickListener() {
        @Override 
        public void onClick(View v){

            popupwindow();

        }});

这是我的弹出式方法

public void popupwindow() {
        try {
            // We need to get the instance of the LayoutInflater

            LayoutInflater inflater = (LayoutInflater) CalorieAMealsActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View layout = inflater.inflate(R.layout.popupcalendar,
                    (ViewGroup) findViewById(R.id.popcal));

            pwindo = new PopupWindow(layout, 1200, 1500, true);
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

            calendar = (CalendarView) findViewById(R.id.calendar);

            // sets whether to show the week number.
            calendar.setShowWeekNumber(false);

            // sets the first day of week according to Calendar.
            // here we set Monday as the first day of the Calendar
            calendar.setFirstDayOfWeek(2);

            //The background color for the selected week.
            calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));

            //sets the color for the dates of an unfocused month. 
            calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));

            //sets the color for the separator line between weeks.
            calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));

            //sets the color for the vertical bar shown at the beginning and at the end of the selected date.
            calendar.setSelectedDateVerticalBar(R.color.darkgreen);

            //sets the listener to be notified upon selected date change.
            calendar.setOnDateChangeListener(new OnDateChangeListener() {
                //show the selected date as a toast
                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                    Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
                }
            });

            btnSubmit = (Button) layout.findViewById(R.id.button2);
            //btnSubmit.setOnClickListener(submit_button_click_listener);

                btnClosePopup = (Button) layout.findViewById(R.id.button1);
                btnClosePopup.setOnClickListener(cancel_button_click_listener);}

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

当我更改日期时,吐司不能正常工作。

1 个答案:

答案 0 :(得分:1)

问题是你需要获得对弹出窗口中膨胀的CalendarView的引用。

在“popupwindow”方法中更改此内容

calendar = (CalendarView) layout.findViewById(R.id.calendar);

您的代码经过此修改:

public void popupwindow() {
    try {
        // We need to get the instance of the LayoutInflater

        LayoutInflater inflater = (LayoutInflater) CalorieAMealsActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.popupcalendar,
                (ViewGroup) findViewById(R.id.popcal));

        pwindo = new PopupWindow(layout, 1200, 1500, true);
        pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

        calendar = (CalendarView) layout.findViewById(R.id.calendar);

        // sets whether to show the week number.
        calendar.setShowWeekNumber(false);

        // sets the first day of week according to Calendar.
        // here we set Monday as the first day of the Calendar
        calendar.setFirstDayOfWeek(2);

        //The background color for the selected week.
        calendar.setSelectedWeekBackgroundColor(getResources().getColor(R.color.green));

        //sets the color for the dates of an unfocused month. 
        calendar.setUnfocusedMonthDateColor(getResources().getColor(R.color.transparent));

        //sets the color for the separator line between weeks.
        calendar.setWeekSeparatorLineColor(getResources().getColor(R.color.transparent));

        //sets the color for the vertical bar shown at the beginning and at the end of the selected date.
        calendar.setSelectedDateVerticalBar(R.color.darkgreen);

        //sets the listener to be notified upon selected date change.
        calendar.setOnDateChangeListener(new OnDateChangeListener() {
            //show the selected date as a toast
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });

        btnSubmit = (Button) layout.findViewById(R.id.button2);
        //btnSubmit.setOnClickListener(submit_button_click_listener);

            btnClosePopup = (Button) layout.findViewById(R.id.button1);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);}

    } catch (Exception e) {
        e.printStackTrace();
    }
}