gpt4 book ai didi

java - Android 日历 - 从星期一开始一周

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:28 25 4
gpt4 key购买 nike

我制作了一个日历,就像屏幕截图上显示的那样。但是我需要从星期一而不是星期日开始那一周...我绑定(bind)了 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;
}

最佳答案

不知道我说的对不对,现在没有 android/java 来测试。

似乎您在 _calendar 上做了一些数学运算,然后将其克隆到 get7x6DayArray 中,在该克隆中设置为从星期一开始,它们返回要显示的天数 []。但是您从未将 _calendar 设置为从星期一开始,所以您会得到从星期日开始的“shortWeekDayNames”(这可能是您的区域设置)。

编辑:仔细研究您的问题:在您的 CalendarWrapper 中,您创建了一个数组来保存星期几的名称:_shortDayNames。

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

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

这不是问题所在吗?

关于java - Android 日历 - 从星期一开始一周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com