1

我按照这个显示日历的教程 http://ashutoshkumarojha.wordpress.com/2013/05/23/custom-calendar-in-android/ 下面是源代码,每个都可以正常工作我只是想自定义这个应用程序,就像这个图像http://imgur.com/5tp1klV 显示只有星期一到星期五,并且有 5 个日期,而不是一个月的 30 天,我该怎么做?下面是完整的源代码

     public class MyCalendarActivity extends Activity implements 
      OnClickListener {
private static final String tag = "MyCalendarActivity";

private TextView currentMonth;
private Button selectedDayMonthYearButton;
private ImageView prevMonth;
private ImageView nextMonth;
private GridView calendarView;
private GridCellAdapter adapter;
private Calendar _calendar;
@SuppressLint("NewApi")
private int month, year;
@SuppressWarnings("unused")
@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
private final DateFormat dateFormatter = new DateFormat();
private static final String dateTemplate = "MMMM yyyy";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_calendar_view);

    _calendar = Calendar.getInstance(Locale.getDefault());
    month = _calendar.get(Calendar.MONTH) + 1;
    year = _calendar.get(Calendar.YEAR);
    Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
            + year);

    selectedDayMonthYearButton = (Button) this
            .findViewById(R.id.selectedDayMonthYear);
    selectedDayMonthYearButton.setText("Selected: ");

    prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
    prevMonth.setOnClickListener(this);

    currentMonth = (TextView) this.findViewById(R.id.currentMonth);
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));

    nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
    nextMonth.setOnClickListener(this);

    calendarView = (GridView) this.findViewById(R.id.calendar);

    // Initialised
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

/**
 * 
 * @param month
 * @param year
 */
private void setGridCellAdapterToDate(int month, int year) {
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

@Override
public void onClick(View v) {
    if (v == prevMonth) {
        if (month <= 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
                + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }
    if (v == nextMonth) {
        if (month > 11) {
            month = 1;
            year++;
        } else {
            month++;
        }
        Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
                + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }

}

@Override
public void onDestroy() {
    Log.d(tag, "Destroying View ...");
    super.onDestroy();
}

// Inner Class
public class GridCellAdapter extends BaseAdapter implements OnClickListener {
    private static final String tag = "GridCellAdapter";
    private final Context _context;

    private final List<String> list;
    private static final int DAY_OFFSET = 1;
    private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
            "Wed", "Thu", "Fri", "Sat" };

    private final String[] months = { "January", "February", "March",
            "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };
    private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
            31, 30, 31 };
    private int daysInMonth;
    private int currentDayOfMonth;
    private int currentWeekDay;
    private Button gridcell;
    private TextView num_events_per_day;
    private final HashMap<String, Integer> eventsPerMonthMap;
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
            "dd-MMM-yyyy");

    // Days in Current Month
    public GridCellAdapter(Context context, int textViewResourceId,
            int month, int year) {
        super();
        this._context = context;
        this.list = new ArrayList<String>();
        Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
                + "Year: " + year);
        Calendar calendar = Calendar.getInstance();
        setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
        Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
        Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
        Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

        // Print Month
        printMonth(month, year);

        // Find Number of Events
        eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
    }

    private String getMonthAsString(int i) {
        return months[i];
    }

    private String getWeekDayAsString(int i) {
        return weekdays[i];
    }

    private int getNumberOfDaysOfMonth(int i) {
        return daysOfMonth[i];
    }

    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * Prints Month
     * 
     * @param mm
     * @param yy
     */
    private void printMonth(int mm, int yy) {
        Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
        int trailingSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

        int currentMonth = mm - 1;
        String currentMonthName = getMonthAsString(currentMonth);
        daysInMonth = getNumberOfDaysOfMonth(currentMonth);

        Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
                + daysInMonth + " days.");

        GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
        Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

        if (currentMonth == 11) {
            prevMonth = currentMonth - 1;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 0;
            prevYear = yy;
            nextYear = yy + 1;

         Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        } else if (currentMonth == 0) {
            prevMonth = 11;
            prevYear = yy - 1;
            nextYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 1;
            Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        } else {
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = yy;
            prevYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
                    + prevMonth + " NextMonth: " + nextMonth
                    + " NextYear: " + nextYear);
        }

        int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
        trailingSpaces = currentWeekDay;

        Log.d(tag, "Week Day:" + currentWeekDay + " is "
                + getWeekDayAsString(currentWeekDay));
        Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
        Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

        if (cal.isLeapYear(cal.get(Calendar.YEAR)))
            if (mm == 2)
                ++daysInMonth;
            else if (mm == 3)
                ++daysInPrevMonth;

        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {
            Log.d(tag,
                    "PREV MONTH:= "
                            + prevMonth
                            + " => "
                            + 
   getMonthAsString(prevMonth)
                            + " "
                            + 
      String.valueOf((daysInPrevMonth
                                    - 
      trailingSpaces + DAY_OFFSET)
                                    + i));
            list.add(String
                    .valueOf((daysInPrevMonth - trailingSpaces 
         + DAY_OFFSET)
                            + i)
                    + "-GREY"
                    + "-"
                    + getMonthAsString(prevMonth)
                    + "-"
                    + prevYear);
        }

        // Current Month Days
        for (int i = 1; i <= daysInMonth; i++) {
            Log.d(currentMonthName, String.valueOf(i) + " "
                    + getMonthAsString(currentMonth) + " " +  
  yy);
            if (i == getCurrentDayOfMonth()) {
                list.add(String.valueOf(i) + "-BLUE" + "-"
                        + getMonthAsString(currentMonth) + 
     "-" + yy);
            } else {
                list.add(String.valueOf(i) + "-WHITE" + "-"
                        + getMonthAsString(currentMonth) + 
    "-" + yy);
            }
        }

        // Leading Month days
        for (int i = 0; i < list.size() % 7; i++) {
            Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
            list.add(String.valueOf(i + 1) + "-GREY" + "-"
                    + getMonthAsString(nextMonth) + "-" + 
    nextYear);
        }
    }

    /**
     * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
     * ALL entries from a SQLite database for that month. Iterate over the
     * List of All entries, and get the dateCreated, which is converted into
     * day.
     * 
     * @param year
     * @param month
     * @return
     */
    private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
            int month) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        return map;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) _context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.screen_gridcell, parent, 
      false);
        }

        // Get a reference to the Day gridcell
        gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
        gridcell.setOnClickListener(this);

        // ACCOUNT FOR SPACING

        Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
        String[] day_color = list.get(position).split("-");
        String theday = day_color[0];
        String themonth = day_color[2];
        String theyear = day_color[3];
        if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) 
  {
            if (eventsPerMonthMap.containsKey(theday)) {
                num_events_per_day = (TextView) row

 .findViewById(R.id.num_events_per_day);
                Integer numEvents = (Integer) 
    eventsPerMonthMap.get(theday);
                num_events_per_day.setText(numEvents.toString());
            }
        }

        // Set the Day GridCell
        gridcell.setText(theday);
        gridcell.setTag(theday + "-" + themonth + "-" + theyear);
        Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
                + theyear);

        if (day_color[1].equals("GREY")) {
            gridcell.setTextColor(getResources()
                    .getColor(R.color.lightgray));
        }
        if (day_color[1].equals("WHITE")) {
            gridcell.setTextColor(getResources().getColor(
                    R.color.lightgray02));
        }
        if (day_color[1].equals("BLUE")) {

        gridcell.setTextColor(getResources().getColor(R.color.orrange));
        }
        return row;
    }

    @Override
    public void onClick(View view) {
        String date_month_year = (String) view.getTag();
        selectedDayMonthYearButton.setText("Selected: " + date_month_year);
        Log.e("Selected date", date_month_year);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());

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

    public int getCurrentDayOfMonth() {
        return currentDayOfMonth;
    }

    private void setCurrentDayOfMonth(int currentDayOfMonth) {
        this.currentDayOfMonth = currentDayOfMonth;
    }

    public void setCurrentWeekDay(int currentWeekDay) {
        this.currentWeekDay = currentWeekDay;
    }

    public int getCurrentWeekDay() {
        return currentWeekDay;
    }
}
         }
4

1 回答 1

0

我解决了这个问题。这是代码:

public class MainActivity extends Activity {
  private int currentWeekDayMain;
  private Button btnPrev, btnNext, btnDate1, btnDate2, btnDate3, btnDate4, btnDate5, btnDate6, btnDate7;
  private TextView txtvMonth;
  private Calendar calTodayMain, calToday, now1, now2, now3, now4, now5, now6, now7;
  private String strDate;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnPrev = (Button) findViewById(R.id.btnPrev);
    btnNext = (Button) findViewById(R.id.btnNext);
    btnDate1 = (Button) findViewById(R.id.btnDate1);
    btnDate2 = (Button) findViewById(R.id.btnDate2);
    btnDate3 = (Button) findViewById(R.id.btnDate3);
    btnDate4 = (Button) findViewById(R.id.btnDate4);
    btnDate5 = (Button) findViewById(R.id.btnDate5);
    btnDate6 = (Button) findViewById(R.id.btnDate6);
    btnDate7 = (Button) findViewById(R.id.btnDate7);
    txtvMonth = (TextView) findViewById(R.id.txtvMonth);
    calTodayMain = Calendar.getInstance();
    currentWeekDayMain = calTodayMain.get(Calendar.DAY_OF_WEEK);
    calToday = Calendar.getInstance();
    now1 = Calendar.getInstance(); now2 = Calendar.getInstance(); now3 = Calendar.getInstance();
    now4 = Calendar.getInstance(); now5 = Calendar.getInstance(); now6 = Calendar.getInstance(); now7 = Calendar.getInstance();
    monthPrint();
    if(currentWeekDayMain == 1) {
      String[] strDateOrig1 = strDate.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      now2.add(Calendar.DATE, +1); now3.add(Calendar.DATE, +2); now4.add(Calendar.DATE, +3); now5.add(Calendar.DATE, +4);
      now6.add(Calendar.DATE, +5);
      now7.add(Calendar.DATE, +6);
      String strDate2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
      String strDate3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
      String strDate4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
      String strDate5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
      String strDate6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
      String strDate7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
      String[] strDateOrig2 = strDate2.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      String[] strDateOrig3 = strDate3.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      String[] strDateOrig4 = strDate4.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      String[] strDateOrig5 = strDate5.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      String[] strDateOrig6 = strDate6.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
      String[] strDateOrig7 = strDate7.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
    } else if(currentWeekDayMain == 2) {
      String[] strDateOrig2 = strDate.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      now1.add(Calendar.DATE, -1); now3.add(Calendar.DATE, +1); now4.add(Calendar.DATE, +2);
      now5.add(Calendar.DATE, +3); now6.add(Calendar.DATE, +4); now7.add(Calendar.DATE, +5);
      String strDate1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
      String strDate3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
      String strDate4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
      String strDate5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
      String strDate6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
      String strDate7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
      String[] strDateOrig1 = strDate1.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      String[] strDateOrig3 = strDate3.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      String[] strDateOrig4 = strDate4.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      String[] strDateOrig5 = strDate5.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      String[] strDateOrig6 = strDate6.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
      String[] strDateOrig7 = strDate7.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
    } else if(currentWeekDayMain == 3) {
      String[] strDateOrig3 = strDate.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      now1.add(Calendar.DATE, -2); now2.add(Calendar.DATE, -1); now4.add(Calendar.DATE, +1);
      now5.add(Calendar.DATE, +2); now6.add(Calendar.DATE, +3); now7.add(Calendar.DATE, +4);
      String strDate1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
      String strDate2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
      String strDate4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
      String strDate5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
      String strDate6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
      String strDate7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
      String[] strDateOrig1 = strDate1.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      String[] strDateOrig2 = strDate2.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      String[] strDateOrig4 = strDate4.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      String[] strDateOrig5 = strDate5.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      String[] strDateOrig6 = strDate6.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
      String[] strDateOrig7 = strDate7.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
    } else if(currentWeekDayMain == 4) {
      String[] strDateOrig4 = strDate.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      now1.add(Calendar.DATE, -3); now2.add(Calendar.DATE, -2); now3.add(Calendar.DATE, -1);
      now5.add(Calendar.DATE, +1); now6.add(Calendar.DATE, +2); now7.add(Calendar.DATE, +3);
      String strDate1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
      String strDate2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
      String strDate3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
      String strDate5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
      String strDate6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
      String strDate7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
      String[] strDateOrig1 = strDate1.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      String[] strDateOrig2 = strDate2.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      String[] strDateOrig3 = strDate3.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      String[] strDateOrig5 = strDate5.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      String[] strDateOrig6 = strDate6.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
      String[] strDateOrig7 = strDate7.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
    } else if(currentWeekDayMain == 5) {
      String[] strDateOrig5 = strDate.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      now1.add(Calendar.DATE, -4); now2.add(Calendar.DATE, -3); now3.add(Calendar.DATE, -2);
      now4.add(Calendar.DATE, -1); now6.add(Calendar.DATE, +1); now7.add(Calendar.DATE, +2);
      String strDate1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
      String strDate2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
      String strDate3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
      String strDate4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
      String strDate6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
      String strDate7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
      String[] strDateOrig1 = strDate1.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      String[] strDateOrig2 = strDate2.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      String[] strDateOrig3 = strDate3.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      String[] strDateOrig4 = strDate4.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      String[] strDateOrig6 = strDate6.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
      String[] strDateOrig7 = strDate7.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
    } else if(currentWeekDayMain == 6) {
      String[] strDateOrig6 = strDate.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
      now1.add(Calendar.DATE, -5); now2.add(Calendar.DATE, -4); now3.add(Calendar.DATE, -3);
      now4.add(Calendar.DATE, -2); now5.add(Calendar.DATE, -1); now7.add(Calendar.DATE, 1);
      String strDate1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
      String strDate2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
      String strDate3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
      String strDate4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
      String strDate5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
      String strDate7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
      String[] strDateOrig1 = strDate1.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      String[] strDateOrig2 = strDate2.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      String[] strDateOrig3 = strDate3.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      String[] strDateOrig4 = strDate4.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      String[] strDateOrig5 = strDate5.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      String[] strDateOrig7 = strDate7.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
    } else if(currentWeekDayMain == 7) {
      String[] strDateOrig7 = strDate.split("-");
      String strDateHere7 = strDateOrig7[2];
      btnDate7.setText(strDateHere7);
      now1.add(Calendar.DATE, -6); now2.add(Calendar.DATE, -5); now3.add(Calendar.DATE, -4);
      now4.add(Calendar.DATE, -3); now5.add(Calendar.DATE, -2); now6.add(Calendar.DATE, -1);
      String strDate1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
      String strDate2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
      String strDate3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
      String strDate4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
      String strDate5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
      String strDate6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
      String[] strDateOrig1 = strDate1.split("-");
      String strDateHere1 = strDateOrig1[2];
      btnDate1.setText(strDateHere1);
      String[] strDateOrig2 = strDate2.split("-");
      String strDateHere2 = strDateOrig2[2];
      btnDate2.setText(strDateHere2);
      String[] strDateOrig3 = strDate3.split("-");
      String strDateHere3 = strDateOrig3[2];
      btnDate3.setText(strDateHere3);
      String[] strDateOrig4 = strDate4.split("-");
      String strDateHere4 = strDateOrig4[2];
      btnDate4.setText(strDateHere4);
      String[] strDateOrig5 = strDate5.split("-");
      String strDateHere5 = strDateOrig5[2];
      btnDate5.setText(strDateHere5);
      String[] strDateOrig6 = strDate6.split("-");
      String strDateHere6 = strDateOrig6[2];
      btnDate6.setText(strDateHere6);
    }
    btnPrev.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        now1.add(Calendar.DATE, -7); now2.add(Calendar.DATE, -7); now3.add(Calendar.DATE, -7);
        now4.add(Calendar.DATE, -7); now5.add(Calendar.DATE, -7); now6.add(Calendar.DATE, -7);
        now7.add(Calendar.DATE, -7); calToday.add(Calendar.DATE, -7);
        DatePrint();
      }
    });
    btnNext.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        now1.add(Calendar.DATE, 7); now2.add(Calendar.DATE, 7); now3.add(Calendar.DATE, 7);
        now4.add(Calendar.DATE, 7); now5.add(Calendar.DATE, 7); now6.add(Calendar.DATE, 7);
        now7.add(Calendar.DATE, 7);
        calToday.add(Calendar.DATE, 7);
        DatePrint();
      }
    });
  }  

  private void DatePrint() {
    String strDay1 = (now1.get(Calendar.YEAR) + "-" + (now1.get(Calendar.MONTH) + 1) + "-" + now1.get(Calendar.DATE));
    String[] strDateOrig1 = strDay1.split("-");
    String strDate1 = strDateOrig1[2];    
    String strDay2 = (now2.get(Calendar.YEAR) + "-" + (now2.get(Calendar.MONTH) + 1) + "-" + now2.get(Calendar.DATE));
    String[] strDateOrig2 = strDay2.split("-");
    String strDate2 = strDateOrig2[2];
    String strDay3 = (now3.get(Calendar.YEAR) + "-" + (now3.get(Calendar.MONTH) + 1) + "-" + now3.get(Calendar.DATE));
    String[] strDateOrig3 = strDay3.split("-");
    String strDate3 = strDateOrig3[2];
    String strDay4 = (now4.get(Calendar.YEAR) + "-" + (now4.get(Calendar.MONTH) + 1) + "-" + now4.get(Calendar.DATE));
    String[] strDateOrig4 = strDay4.split("-");
    String strDate4 = strDateOrig4[2];
    String strDay5 = (now5.get(Calendar.YEAR) + "-" + (now5.get(Calendar.MONTH) + 1) + "-" + now5.get(Calendar.DATE));
    String[] strDateOrig5 = strDay5.split("-");
    String strDate5 = strDateOrig5[2];
    String strDay6 = (now6.get(Calendar.YEAR) + "-" + (now6.get(Calendar.MONTH) + 1) + "-" + now6.get(Calendar.DATE));
    String[] strDateOrig6 = strDay6.split("-");
    String strDate6 = strDateOrig6[2];
    String strDay7 = (now7.get(Calendar.YEAR) + "-" + (now7.get(Calendar.MONTH) + 1) + "-" + now7.get(Calendar.DATE));
    String[] strDateOrig7 = strDay7.split("-");
    String strDate7 = strDateOrig7[2];
    btnDate1.setText(strDate1); btnDate2.setText(strDate2); btnDate3.setText(strDate3);
    btnDate4.setText(strDate4); btnDate5.setText(strDate5); btnDate6.setText(strDate6);
    btnDate7.setText(strDate7); txtvMonth.setTextColor(Color.BLUE);
    if(strDate1 == "1" || strDate1.equals("1")) {
      txtvMonth.setText(calendarMonth((now1.get(Calendar.MONTH) + 1)));
    } else if(strDate2 == "1" || strDate2.equals("1")) {
      txtvMonth.setText(calendarMonth((now2.get(Calendar.MONTH) + 1)));
    } else if(strDate3 == "1" || strDate3.equals("1")) {
      txtvMonth.setText(calendarMonth((now3.get(Calendar.MONTH) + 1)));
    } else if(strDate4 == "1" || strDate4.equals("1")) {
      txtvMonth.setText(calendarMonth((now4.get(Calendar.MONTH) + 1)));
    } else if(strDate5 == "1" || strDate5.equals("1")) {
      txtvMonth.setText(calendarMonth((now5.get(Calendar.MONTH) + 1)));
    } else if(strDate6 == "1" || strDate6.equals("1")) {
      txtvMonth.setText(calendarMonth((now6.get(Calendar.MONTH) + 1)));
    } else if(strDate7 == "1" || strDate7.equals("1")) {
      txtvMonth.setText(calendarMonth((now7.get(Calendar.MONTH) + 1)));
    } else
    monthPrint();
  }

  private CharSequence calendarMonth(int i) {
    String strMonth;
    if(i == 1) {
      strMonth = "January";
    } else if(i == 2) {
      strMonth = "February";
    } else if(i == 3) {
      strMonth = "March";
    } else if(i == 4) {
      strMonth = "April";
    } else if(i == 5) {
      strMonth = "May";
    } else if(i == 6) {
      strMonth = "June";
    } else if(i == 7) {
      strMonth = "July";
    } else if(i == 8) {
      strMonth = "August";
    } else if(i == 9) {
      strMonth = "September";
    } else if(i == 10) {
      strMonth = "October";
    } else if(i == 11) {
      strMonth = "November";
    } else {
      strMonth = "December";
    }
    return strMonth;
  }

  private void monthPrint() {
    strDate = (calToday.get(Calendar.YEAR) + "-" + (calToday.get(Calendar.MONTH) + 1) + "-" + calToday.get(Calendar.DATE));
    if((calToday.get(Calendar.MONTH) + 1) == 1) {
      txtvMonth.setTextColor(Color.BLUE);
      txtvMonth.setText("January");
    } else if((calToday.get(Calendar.MONTH) + 1) == 2) {
      txtvMonth.setTextColor(Color.BLUE);
      txtvMonth.setText("February");
    } else if((calToday.get(Calendar.MONTH) + 1) == 3) {
      txtvMonth.setText("March");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 4) {
      txtvMonth.setText("April");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 5) {
      txtvMonth.setText("May");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 6) {
      txtvMonth.setText("June");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 7) {
      txtvMonth.setText("July");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 8) {
      txtvMonth.setText("August");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 9) {
      txtvMonth.setText("September");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 10) {
      txtvMonth.setText("October");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 11) {
      txtvMonth.setText("November");
      txtvMonth.setTextColor(Color.BLUE);
    } else if((calToday.get(Calendar.MONTH) + 1) == 12) {
      txtvMonth.setText("December");
      txtvMonth.setTextColor(Color.BLUE);
    }
  }
}

以及附在此处的 xml 文件 - activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txtvMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnPrev"
            android:layout_width="35sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="@string/left"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay1"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="S"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay2"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="M"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay3"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="T"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay4"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="W"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay5"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="T"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay6"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="F"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDay7"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="S"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="35sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:text="@string/next"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnBorderLeft"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate1"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate2"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate3"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate4"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate5"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate6"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnDate7"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btnBorderRight"
            android:layout_width="32sp"
            android:layout_height="32sp"
            android:background="@drawable/tag_bg"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>
于 2015-01-08T13:24:40.393 回答