2011-06-20 85 views
5

我製作了一個日曆,就像它在屏幕截圖中顯示的那樣..但是我需要做的那一週是星期一而不是星期日開始...我綁在做Calendar cal = Calendar.getInstanty(); cal.setFirstDayOfWeek(Calendar.MONDAY); 但這沒有幫助..任何想法?Android日曆 - 星期一開始爲

感謝您

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

不知道我是對的,沒有的Android/java的,現在來測試。

似乎你在_calendar上做了一些數學計算,然後將它克隆到get7x6DayArray中,設置爲在該克隆週一開始,它們返回要顯示的天數[]。但是您從未將星期一開始設置爲_calendar,因此您可以在星期日開始(這可能是您的語言環境的設置)開始的「shortWeekDayNames」。

編輯:仔細看看你的問題:在你的CalendarWrapper你創建一個數組來保存星期幾的日期:_shortDayNames。

然後你做一個循環來填充短名稱的數組:你從0-6循環,得到1-7天的名字。

如果你仔細看看thisthis,你會發現星期日始終是值1:calendar.SUNDAY = 1,所以_shortDayNames [0]總是得到對應於「1」值的字符串,這總是星期天。所以當您顯示日曆時,您的日曆始終顯示爲「星期天」。

這不是問題嗎?

+0

感謝您的回覆,並試圖幫助:)理論上我需要在類CalendarWrapper中設置_calendar.setFirstDayOfWeek(2)或不?如果是的話,這沒有幫助.. – Sergio

+0

@Sergio:看看編輯答案... – woliveirajr

+0

Yeaah ...非常感謝!..你真的向我展示了我的錯誤..你的回答真的幫助了! – Sergio

相關問題