2016-04-30 79 views
1

我正在使用自定義日曆從此link。它的工作,但本月除外的所有月份的日期已變灰。我希望顯示所有月份,如第二張屏幕截圖所示。但除了當前月份(即4月)以外,所有月份都會變灰。任何人都可以幫助我像第二張截圖一樣顯示每個月,而不是像第一張截圖一樣?setEmptyView當edittext爲空時使用textwatcher

Calendar View: 
 

 
public class CalendarView extends LinearLayout 
 
{ 
 
\t // for logging 
 
\t private static final String LOGTAG = "Calendar View"; 
 

 
\t // how many days to show, defaults to six weeks, 42 days 
 
\t private static final int DAYS_COUNT = 42; 
 

 
\t // default date format 
 
\t private static final String DATE_FORMAT = "MMM yyyy"; 
 

 
\t // date format 
 
\t private String dateFormat; 
 

 
\t // current displayed month 
 
\t private Calendar currentDate = Calendar.getInstance(); 
 

 
\t //event handling 
 
\t private EventHandler eventHandler = null; 
 

 
\t // internal components 
 
\t private LinearLayout header; 
 
\t private ImageView btnPrev; 
 
\t private ImageView btnNext; 
 
\t private TextView txtDate; 
 
\t private GridView grid; 
 

 
\t // seasons' rainbow 
 
\t int[] rainbow = new int[] { 
 
\t \t \t R.color.summer, 
 
\t \t \t R.color.fall, 
 
\t \t \t R.color.winter, 
 
\t \t \t R.color.spring 
 
\t }; 
 

 
\t // month-season association (northern hemisphere, sorry australia :) 
 
\t int[] monthSeason = new int[] {2, 2, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2}; 
 

 
\t public CalendarView(Context context) 
 
\t { 
 
\t \t super(context); 
 
\t } 
 

 
\t public CalendarView(Context context, AttributeSet attrs) 
 
\t { 
 
\t \t super(context, attrs); 
 
\t \t initControl(context, attrs); 
 
\t } 
 

 
\t public CalendarView(Context context, AttributeSet attrs, int defStyleAttr) 
 
\t { 
 
\t \t super(context, attrs, defStyleAttr); 
 
\t \t initControl(context, attrs); 
 
\t } 
 

 
\t /** 
 
\t * Load control xml layout 
 
\t */ 
 
\t private void initControl(Context context, AttributeSet attrs) 
 
\t { 
 
\t \t LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
\t \t inflater.inflate(R.layout.control_calendar, this); 
 

 
\t \t loadDateFormat(attrs); 
 
\t \t assignUiElements(); 
 
\t \t assignClickHandlers(); 
 

 
\t \t updateCalendar(); 
 
\t } 
 

 
\t private void loadDateFormat(AttributeSet attrs) 
 
\t { 
 
\t \t TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CalendarView); 
 

 
\t \t try 
 
\t \t { 
 
\t \t \t // try to load provided date format, and fallback to default otherwise 
 
\t \t \t dateFormat = ta.getString(R.styleable.CalendarView_dateFormat); 
 
\t \t \t if (dateFormat == null) 
 
\t \t \t \t dateFormat = DATE_FORMAT; 
 
\t \t } 
 
\t \t finally 
 
\t \t { 
 
\t \t \t ta.recycle(); 
 
\t \t } 
 
\t } 
 
\t private void assignUiElements() 
 
\t { 
 
\t \t // layout is inflated, assign local variables to components 
 
\t \t header = (LinearLayout)findViewById(R.id.calendar_header); 
 
\t \t btnPrev = (ImageView)findViewById(R.id.calendar_prev_button); 
 
\t \t btnNext = (ImageView)findViewById(R.id.calendar_next_button); 
 
\t \t txtDate = (TextView)findViewById(R.id.calendar_date_display); 
 
\t \t grid = (GridView)findViewById(R.id.calendar_grid); 
 
\t } 
 

 
\t private void assignClickHandlers() 
 
\t { 
 
\t \t // add one month and refresh UI 
 
\t \t btnNext.setOnClickListener(new OnClickListener() 
 
\t \t { 
 
\t \t \t @Override 
 
\t \t \t public void onClick(View v) 
 
\t \t \t { 
 
\t \t \t \t currentDate.add(Calendar.MONTH, 1); 
 
\t \t \t \t updateCalendar(); 
 
\t \t \t } 
 
\t \t }); 
 

 
\t \t // subtract one month and refresh UI 
 
\t \t btnPrev.setOnClickListener(new OnClickListener() 
 
\t \t { 
 
\t \t \t @Override 
 
\t \t \t public void onClick(View v) 
 
\t \t \t { 
 
\t \t \t \t currentDate.add(Calendar.MONTH, -1); 
 
\t \t \t \t updateCalendar(); 
 
\t \t \t } 
 
\t \t }); 
 

 
\t \t // long-pressing a day 
 
\t \t grid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
 
\t \t { 
 

 
\t \t \t @Override 
 
\t \t \t public boolean onItemLongClick(AdapterView<?> view, View cell, int position, long id) 
 
\t \t \t { 
 
\t \t \t \t // handle long-press 
 
\t \t \t \t if (eventHandler == null) 
 
\t \t \t \t \t return false; 
 

 
\t \t \t \t eventHandler.onDayLongPress((Date)view.getItemAtPosition(position)); 
 
\t \t \t \t return true; 
 
\t \t \t } 
 
\t \t }); 
 
\t } 
 

 
\t /** 
 
\t * Display dates correctly in grid 
 
\t */ 
 
\t public void updateCalendar() 
 
\t { 
 
\t \t updateCalendar(null); 
 
\t } 
 

 
\t /** 
 
\t * Display dates correctly in grid 
 
\t */ 
 
\t public void updateCalendar(HashSet<Date> events) 
 
\t { 
 
\t \t ArrayList<Date> cells = new ArrayList<>(); 
 
\t \t Calendar calendar = (Calendar)currentDate.clone(); 
 

 
\t \t // determine the cell for current month's beginning 
 
\t \t calendar.set(Calendar.DAY_OF_MONTH, 1); 
 
\t \t int monthBeginningCell = calendar.get(Calendar.DAY_OF_WEEK) - 1; 
 

 
\t \t // move calendar backwards to the beginning of the week 
 
\t \t calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell); 
 

 
\t \t // fill cells 
 
\t \t while (cells.size() < DAYS_COUNT) 
 
\t \t { 
 
\t \t \t cells.add(calendar.getTime()); 
 
\t \t \t calendar.add(Calendar.DAY_OF_MONTH, 1); 
 
\t \t } 
 

 
\t \t // update grid 
 
\t \t grid.setAdapter(new CalendarAdapter(getContext(), cells, events)); 
 

 
\t \t // update title 
 
\t \t SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); 
 
\t \t txtDate.setText(sdf.format(currentDate.getTime())); 
 

 
\t \t // set header color according to current season 
 
\t \t int month = currentDate.get(Calendar.MONTH); 
 
\t \t int season = monthSeason[month]; 
 
\t \t int color = rainbow[season]; 
 

 
\t \t header.setBackgroundColor(getResources().getColor(color)); 
 
\t } 
 

 

 
\t private class CalendarAdapter extends ArrayAdapter<Date> 
 
\t { 
 
\t \t // days with events 
 
\t \t private HashSet<Date> eventDays; 
 

 
\t \t // for view inflation 
 
\t \t private LayoutInflater inflater; 
 

 
\t \t public CalendarAdapter(Context context, ArrayList<Date> days, HashSet<Date> eventDays) 
 
\t \t { 
 
\t \t \t super(context, R.layout.control_calendar_day, days); 
 
\t \t \t this.eventDays = eventDays; 
 
\t \t \t inflater = LayoutInflater.from(context); 
 
\t \t } 
 

 
\t \t @Override 
 
\t \t public View getView(int position, View view, ViewGroup parent) 
 
\t \t { 
 
\t \t \t // day in question 
 
\t \t \t Date date = getItem(position); 
 
\t \t \t int day = date.getDate(); 
 
\t \t \t int month = date.getMonth(); 
 
\t \t \t int year = date.getYear(); 
 

 
\t \t \t // today 
 
\t \t \t Date today = new Date(); 
 

 
\t \t \t // inflate item if it does not exist yet 
 
\t \t \t if (view == null) 
 
\t \t \t \t view = inflater.inflate(R.layout.control_calendar_day, parent, false); 
 

 
\t \t \t // if this day has an event, specify event image 
 
\t \t \t view.setBackgroundResource(0); 
 
\t \t \t if (eventDays != null) 
 
\t \t \t { 
 
\t \t \t \t for (Date eventDate : eventDays) 
 
\t \t \t \t { 
 
\t \t \t \t \t if (eventDate.getDate() == day && 
 
\t \t \t \t \t \t \t eventDate.getMonth() == month && 
 
\t \t \t \t \t \t \t eventDate.getYear() == year) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t // mark this day for event 
 
\t \t \t \t \t \t view.setBackgroundResource(R.drawable.reminder); 
 
\t \t \t \t \t \t break; 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 

 
\t \t \t // clear styling 
 
\t \t \t ((TextView)view).setTypeface(null, Typeface.NORMAL); 
 
\t \t \t ((TextView)view).setTextColor(Color.BLACK); 
 

 
\t \t \t if (month != today.getMonth() || year != today.getYear()) 
 
\t \t \t { 
 
\t \t \t \t // if this day is outside current month, grey it out 
 
\t \t \t \t ((TextView)view).setTextColor(getResources().getColor(R.color.greyed_out)); 
 
\t \t \t } 
 
\t \t \t else if (day == today.getDate()) 
 
\t \t \t { 
 
\t \t \t \t // if it is today, set it to blue/bold 
 
\t \t \t \t ((TextView)view).setTypeface(null, Typeface.BOLD); 
 
\t \t \t \t ((TextView)view).setTextColor(getResources().getColor(R.color.today)); 
 
\t \t \t } 
 

 
\t \t \t // set text 
 
\t \t \t ((TextView)view).setText(String.valueOf(date.getDate())); 
 

 
\t \t \t return view; 
 
\t \t } 
 
\t } 
 

 
\t /** 
 
\t * Assign event handler to be passed needed events 
 
\t */ 
 
\t public void setEventHandler(EventHandler eventHandler) 
 
\t { 
 
\t \t this.eventHandler = eventHandler; 
 
\t } 
 

 
\t /** 
 
\t * This interface defines what events to be reported to 
 
\t * the outside world 
 
\t */ 
 
\t public interface EventHandler 
 
\t { 
 
\t \t void onDayLongPress(Date date); 
 
\t } 
 
}
使用txtDate.getText

2nd Screenshot[![][2] 1st Screenshot

+0

http://stackoverflow.com/questions/13411685/set-the-text-color-of-calendar-view-month-name –

+0

感謝您的快速回復。請檢查getView()中的條件'if(month!= today.getMonth()|| year!= today.getYear())'。這是灰化日期的原因。 –

回答

0

獲取通過在heade設置日期,();

SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); 
    Date currentSelectedDate = sdf.parse(using txtDate.getText()); 


if (month != currentSelectedDate.getMonth() || year != currentSelectedDate.getYear()) 
相關問題