Android日历 - 星期一开始一周

时间:2011-06-20 21:24:37

标签: java android calendar

我制作了一个日历,就像它显示在屏幕截图上..但我需要在星期一开始,而不是星期日......我与Calendar cal = Calendar.getInstanty(); cal.setFirstDayOfWeek(Calendar.MONDAY);绑在一起 但这没有帮助..任何想法?

谢谢你

enter image description here

    import java.util.Calendar;
import java.util.Locale;

    import android.content.Context;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.Spinner;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    import android.text.format.DateFormat;
import android.text.format.DateUtils;


    public class CalendarView extends LinearLayout {
        public CalendarView(Context context) {
            super(context);
            init(context);
        }



        public CalendarView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public interface OnMonthChangedListener {
            public void onMonthChanged(CalendarView view);
        }

        public void setOnMonthChangedListener(OnMonthChangedListener l) {
            _onMonthChangedListener = l;
        }

        public interface OnSelectedDayChangedListener {
            public void onSelectedDayChanged(CalendarView view);
        }

        public void setOnSelectedDayChangedListener(OnSelectedDayChangedListener l) {
            _onSelectedDayChangedListener = l;
        }

        public Calendar getVisibleStartDate() {
            return _calendar.getVisibleStartDate();
        }

        public Calendar getVisibleEndDate() {
            return _calendar.getVisibleEndDate();
        }

        public Calendar getSelectedDay() {
            return _calendar.getSelectedDay();
        }


        private void init(Context context) {
            View v = LayoutInflater.from(context).inflate(R.layout.calendar, this, true);

            _calendar = new CalendarWrapper();
            _days = (TableLayout) v.findViewById(R.id.days);
            _up = (TextView) v.findViewById(R.id.up);
            _prev = (Button) v.findViewById(R.id.previous);
            _next = (Button) v.findViewById(R.id.next);


            refreshCurrentDate();




            // Days Table
            String[] shortWeekDayNames = _calendar.getShortDayNames();
            for (int i = 0; i < 7; i++) { // Rows
                TableRow tr = (TableRow) _days.getChildAt(i);

                for (int j = 0; j < 7; j++) { // Columns
                    Boolean header = i == 0; // First row is weekday headers
                    TextView tv = (TextView) tr.getChildAt(j);

                    if (header)
                        tv.setText(shortWeekDayNames[j]);
                    else
                        tv.setOnClickListener(_dayClicked);
                }
            }

            refreshDayCells();

            // Listeners
            _calendar.setOnDateChangedListener(_dateChanged);
            _prev.setOnClickListener(_incrementClicked);
            _next.setOnClickListener(_incrementClicked);

            setView(MONTH_VIEW);
        }
        private OnClickListener _incrementClicked = new OnClickListener() {
            public void onClick(View v) {
                int inc = (v == _next ? 1 : -1);

                if (_currentView == MONTH_VIEW)
                    _calendar.addMonth(inc);
                else if (_currentView == DAY_VIEW) {
                    _calendar.addDay(inc);
                    invokeSelectedDayChangedListener();
                }
                else if (_currentView == YEAR_VIEW) {
                    _currentYear += inc;
                    refreshUpText();
                }
            }
        };
        private OnDateChangedListener _dateChanged = new OnDateChangedListener() {
            public void onDateChanged(CalendarWrapper sc) {
                Boolean monthChanged = _currentYear != sc.getYear() || _currentMonth != sc.getMonth();

                if (monthChanged) {
                    refreshDayCells();
                    invokeMonthChangedListener();
                }

                refreshCurrentDate();
                refreshUpText();
            }
        };



        private OnClickListener _dayClicked = new OnClickListener() {
            public void onClick(View v) {

            }
        };




        private void refreshDayCells() {
            int[] dayGrid = _calendar.get7x6DayArray();
            int monthAdd = -1;
            int row = 1; // Skip weekday header row
            int col = 0;

            for (int i = 0; i < dayGrid.length; i++) {
                int day = dayGrid[i];

                if (day == 1)
                    monthAdd++;

                TableRow tr = (TableRow) _days.getChildAt(row);
                TextView tv = (TextView) tr.getChildAt(col);

                //Clear current markers, if any.
                tv.setBackgroundDrawable(null);
                tv.setWidth(0);
                tv.setTextSize(12);
                tv.setText(dayGrid[i] + " 100€");
                tv.setBackgroundColor(Color.WHITE);


                if (monthAdd == 0)
                    tv.setTextColor(Color.BLACK);
                else
                    tv.setTextColor(Color.LTGRAY);


                tv.setTag(new int[] { monthAdd, dayGrid[i] });

                col++;

                if (col == 7) {
                    col = 0;
                    row++;
                }
            }
        }

        private void setView(int view) {
            if (_currentView != view) {
                _currentView = view;
                _days.setVisibility(_currentView == MONTH_VIEW ? View.VISIBLE : View.GONE);
                refreshUpText();
            }
        }

        private void refreshUpText() {
            switch (_currentView) {
                case MONTH_VIEW:
                    _up.setText(_calendar.toString("MMMM yyyy"));
                    break;
                case YEAR_VIEW:
                    _up.setText(_currentYear + "");
                    break;
                case CENTURY_VIEW:
                    _up.setText("CENTURY_VIEW");
                    break;
                case DECADE_VIEW:
                    _up.setText("DECADE_VIEW");
                    break;
                case DAY_VIEW:
                    _up.setText(_calendar.toString("EEEE, MMMM dd, yyyy"));
                    break;
                case ITEM_VIEW:
                    _up.setText("ITEM_VIEW");
                    break;
                default:
                    break;
            }
        }

        private void refreshCurrentDate() {
            _currentYear = _calendar.getYear();
            _currentMonth = _calendar.getMonth();
            _calendar.getDay();


            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);
            if(month == _calendar.getMonth() && year== _calendar.getYear()){_prev.setVisibility(INVISIBLE);}
            else {_prev.setVisibility(VISIBLE);}
        }

        private void invokeMonthChangedListener() {
            if (_onMonthChangedListener != null)
                _onMonthChangedListener.onMonthChanged(this);
        }

        private void invokeSelectedDayChangedListener() {
            if (_onSelectedDayChangedListener != null)
                _onSelectedDayChangedListener.onSelectedDayChanged(this);
        }

        private final int CENTURY_VIEW = 5;
        private final int DECADE_VIEW = 4;
        private final int YEAR_VIEW = 3;
        private final int MONTH_VIEW = 2;
        private final int DAY_VIEW = 1;
        private final int ITEM_VIEW = 0;

        private CalendarWrapper _calendar;
        private TableLayout _days;
        private TextView _up;
        private Button _prev;
        private Button _next;
//      private Spinner sailFromSpinner;
        private OnMonthChangedListener _onMonthChangedListener;
        private OnSelectedDayChangedListener _onSelectedDayChangedListener;
        private int _currentView;
        private int _currentYear;
        private int _currentMonth;
        public Calendar cal = Calendar.getInstance();
    }

    class CalendarWrapper {
        public interface OnDateChangedListener {
            public void onDateChanged(CalendarWrapper sc);
        }

        public CalendarWrapper() {
            _calendar = Calendar.getInstance();



            _shortDayNames = new String[_calendar.getActualMaximum(Calendar.DAY_OF_WEEK)];
            _shortMonthNames = new String[_calendar.getActualMaximum(Calendar.MONTH) + 1]; // Months are 0-based so size is Max + 1

            for (int i = 0; i < _shortDayNames.length; i++) {
                _shortDayNames[i] = DateUtils.getDayOfWeekString(i +1 , DateUtils.LENGTH_SHORT);
            }

            for (int i = 0; i < _shortMonthNames.length; i++) {
                _shortMonthNames[i] = DateUtils.getMonthString(i, DateUtils.LENGTH_SHORT);
            }
        }


        public int getYear() {
            return _calendar.get(Calendar.YEAR);
        }

        public int getMonth() {
            return _calendar.get(Calendar.MONTH);
        }

        public int getDayOfWeek() {
            return _calendar.get(Calendar.DAY_OF_WEEK);
        }

        public int getDay() {
            return _calendar.get(Calendar.DAY_OF_MONTH);
        }


        public void setYear(int value) {
            _calendar.set(Calendar.YEAR, value);
            invokeDateChangedListener();
        }

        public void setYearAndMonth(int year, int month) {
            _calendar.set(Calendar.YEAR, year);
            _calendar.set(Calendar.MONTH, month);
            invokeDateChangedListener();
        }

        public void setMonth(int value) {
            _calendar.set(Calendar.MONTH, value);
            invokeDateChangedListener();
        }

        public void setDay(int value) {
            _calendar.set(Calendar.DAY_OF_MONTH, value);
            invokeDateChangedListener();
        }

        public void addYear(int value) {
            if(value != 0) {
                _calendar.add(Calendar.YEAR, value);
                invokeDateChangedListener();
            }
        }

        public void addMonth(int value) {
            if(value != 0) {
                _calendar.add(Calendar.MONTH, value);
                invokeDateChangedListener();
            }
        }

        public void addMonthSetDay(int monthAdd, int day) {
            _calendar.add(Calendar.MONTH, monthAdd);
            _calendar.set(Calendar.DAY_OF_MONTH, day);

            invokeDateChangedListener();
        }

        public void addDay(int value) {
            if(value != 0) {
                _calendar.add(Calendar.DAY_OF_MONTH, value);
                invokeDateChangedListener();
            }
        }



        public String[] getShortDayNames() {
                return _shortDayNames;
        }

        public String[] getShortMonthNames() {
            return _shortMonthNames;
        }

        public int[] get7x6DayArray() {
            _visibleStartDate = null;
            _visibleEndDate = null;

            int[] days = new int[42];

            Calendar tempCal = (Calendar) _calendar.clone();
            tempCal.setFirstDayOfWeek(2);
            tempCal.set(Calendar.DAY_OF_MONTH, 1);

            int dayOfWeekOn1st = tempCal.get(Calendar.DAY_OF_WEEK);
            int maxDay = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH);
            int previousMonthCount = dayOfWeekOn1st - 1;
            int index = 0;

            if (previousMonthCount > 0) {
                tempCal.set(Calendar.DAY_OF_MONTH, -1);

                int previousMonthMax = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH);

                for (int i = previousMonthCount; i > 0; i--) {
                    int day = previousMonthMax - i + 1; 

                    if(i == previousMonthCount) {
                        _visibleStartDate = (Calendar)tempCal.clone();
//                      _visibleStartDate.setFirstDayOfWeek(2);
                        _visibleStartDate.set(Calendar.DAY_OF_MONTH, day);
                    }

                    days[index] = day;
                    index++;
                }
            }

            for (int i = 0; i < maxDay; i++) {
                if(i == 0 && _visibleStartDate == null)
                    _visibleStartDate = (Calendar)tempCal.clone();

                days[index] = (i + 1);
                index++;
            }

            int nextMonthDay = 1;

            for (int i = index; i < days.length; i++) {
                if(i == index)


                days[index] = nextMonthDay;
                nextMonthDay++;
                index++;
            }

            _visibleEndDate = (Calendar) _calendar.clone();
            _visibleEndDate.add(Calendar.MONTH, 1);
            _visibleEndDate.set(Calendar.DAY_OF_MONTH, days[41]);

            return days;
        }

        public Calendar getSelectedDay() {
            return (Calendar)_calendar.clone();
        }

        public Calendar getVisibleStartDate() {
            return (Calendar) _visibleStartDate.clone();
        }

        public Calendar getVisibleEndDate() {
            return (Calendar) _visibleEndDate.clone();
        }

        public void setOnDateChangedListener(OnDateChangedListener l) {
            _onDateChangedListener = l;
        }

        public String toString(CharSequence format) {
            return DateFormat.format(format, _calendar).toString();
        }

        private void invokeDateChangedListener() {
            if (_onDateChangedListener != null)
                _onDateChangedListener.onDateChanged(this);
        }

        private Calendar _calendar;
        private String[] _shortDayNames;
        private String[] _shortMonthNames;
        private OnDateChangedListener _onDateChangedListener;
        private Calendar _visibleStartDate;
        private Calendar _visibleEndDate;
    }

1 个答案:

答案 0 :(得分:1)

不知道我是不对,现在没有android / java进行测试。

看来你在_calendar上做了一些数学运算,然后在get7x6DayArray中克隆它,设置为在该克隆的星期一开始,它们返回要显示的days []。但是你永远不会将_calendar设置为星期一开始,所以你得到的是“shortWeekDayNames”从星期日开始(这可能就是你的语言环境的设置)。

编辑:仔细查看您的问题:在CalendarWrapper中创建一个数组来保存一周中的日期名称:_shortDayNames。

然后你做一个循环用短名称填充数组:你从0-6循环,得到1-7天的名字。

如果仔细观察thisthis,您会看到星期日总是值1:calendar.SUNDAY = 1,所以_shortDayNames [0]将始终获得对应的字符串到“1”值,这总是星期天。因此,当您显示日历时,您的日历将始终显示为“星期日”。

这不是问题吗?