
我在我的应用程序中使用日历,成功地在我的应用程序中实现了日历,但疑问是如何启用和禁用特定日期(例如:需要在所有月份或星期一以及所有月份的星期日仅启用星期一).
我的日历视图
import java.util.ArrayList;import java.util.GregorianCalendar;import java.util.Locale;import androID.app.Activity;import androID.graphics.color;import androID.graphics.Typeface;import androID.os.Bundle;import androID.os.Handler;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.Window;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.GrIDVIEw;import androID.Widget.linearLayout;import androID.Widget.relativeLayout;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.infometricx.adapter.Calendaradapter;import com.infometricx.utils.CalendarUtility;public class CalendarVIEw extends Activity { public GregorianCalendar month, itemmonth;// calendar instances. public Calendaradapter adapter;// adapter instance public Handler handler;// for grabbing some event values for showing the dot // marker. public ArrayList<String> items; // container to store calendar items which // needs showing the event marker ArrayList<String> event; linearLayout rLayout; ArrayList<String> date; ArrayList<String> desc; Typeface MyriadPro; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestwindowFeature(Window.FEATURE_NO_Title); setContentVIEw(R.layout.calender); Locale.setDefault(Locale.US); rLayout = (linearLayout) findVIEwByID(R.ID.text); MyriadPro = Typeface.createFromAsset(getAssets(), "Fonts/MyriadPro-light.otf"); month = (GregorianCalendar) GregorianCalendar.getInstance(); itemmonth = (GregorianCalendar) month.clone(); items = new ArrayList<String>(); adapter = new Calendaradapter(this, month); GrIDVIEw grIDvIEw = (GrIDVIEw) findVIEwByID(R.ID.grIDvIEw); grIDvIEw.setAdapter(adapter); handler = new Handler(); handler.post(calendarUpdater); TextVIEw Title = (TextVIEw) findVIEwByID(R.ID.Title); Title.setText(androID.text.format.DateFormat.format("MMMM yyyy", month)); Title.setTypeface(MyriadPro); relativeLayout prevIoUs = (relativeLayout) findVIEwByID(R.ID.prevIoUs); prevIoUs.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setPrevIoUsMonth(); refreshCalendar(); } }); relativeLayout next = (relativeLayout) findVIEwByID(R.ID.next); next.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setNextMonth(); refreshCalendar(); } }); grIDvIEw.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw v, int position, long ID) { // removing the prevIoUs vIEw if added if (((linearLayout) rLayout).getChildCount() > 0) { ((linearLayout) rLayout).removeAllVIEws(); } desc = new ArrayList<String>(); date = new ArrayList<String>(); ((Calendaradapter) parent.getAdapter()).setSelected(v); String selectedGrIDDate = Calendaradapter.dayString .get(position); Log.d("selectedGrIDDate", "----->" + selectedGrIDDate); String[] separatedTime = selectedGrIDDate.split("-"); String grIDvalueString = separatedTime[2].replaceFirst("^0*", "");// taking last part of date. IE; 2 from 2012-12-02. int grIDvalue = Integer.parseInt(grIDvalueString); // navigate to next or prevIoUs month on clicking offdays. if ((grIDvalue > 10) && (position < 8)) { setPrevIoUsMonth(); refreshCalendar(); } else if ((grIDvalue < 7) && (position > 28)) { setNextMonth(); refreshCalendar(); } ((Calendaradapter) parent.getAdapter()).setSelected(v); for (int i = 0; i < CalendarUtility.startDates.size(); i++) { } for (int i = 0; i < CalendarUtility.startDates.size(); i++) { if (CalendarUtility.startDates.get(i).equals( selectedGrIDDate)) { desc.add(CalendarUtility.nameOfEvent.get(i)); } } if (desc.size() > 0) { for (int i = 0; i < desc.size(); i++) { TextVIEw rowTextVIEw = new TextVIEw(CalendarVIEw.this); // set some propertIEs of rowTextVIEw or something rowTextVIEw.setText("Event:" + desc.get(i)); rowTextVIEw.setTextcolor(color.BLACK); // add the textvIEw to the linearlayout rLayout.addVIEw(rowTextVIEw); } } desc = null; } }); } protected voID setNextMonth() { if (month.get(GregorianCalendar.MONTH) == month .getActualMaximum(GregorianCalendar.MONTH)) { month.set((month.get(GregorianCalendar.YEAR) + 1), month.getActualMinimum(GregorianCalendar.MONTH), 1); } else { month.set(GregorianCalendar.MONTH, month.get(GregorianCalendar.MONTH) + 1); } } protected voID setPrevIoUsMonth() { if (month.get(GregorianCalendar.MONTH) == month .getActualMinimum(GregorianCalendar.MONTH)) { month.set((month.get(GregorianCalendar.YEAR) - 1), month.getActualMaximum(GregorianCalendar.MONTH), 1); } else { month.set(GregorianCalendar.MONTH, month.get(GregorianCalendar.MONTH) - 1); } } protected voID showToast(String string) { Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); } public voID refreshCalendar() { TextVIEw Title = (TextVIEw) findVIEwByID(R.ID.Title); adapter.refreshDays(); adapter.notifyDataSetChanged(); handler.post(calendarUpdater); // generate some calendar items Title.setText(androID.text.format.DateFormat.format("MMMM yyyy", month)); } public Runnable calendarUpdater = new Runnable() { @OverrIDe public voID run() { items.clear(); // Print dates of the current week // DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US); event = CalendarUtility.readCalendarEvent(CalendarVIEw.this); Log.d("=====Event====", event.toString()); Log.d("=====Date ARRAY====", CalendarUtility.startDates.toString()); for (int i = 0; i < CalendarUtility.startDates.size(); i++) { itemmonth.add(GregorianCalendar.DATE, 1); items.add(CalendarUtility.startDates.get(i).toString()); } adapter.setItems(items); adapter.notifyDataSetChanged(); } };}我的日历适配器
import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.GregorianCalendar;import java.util.List;import java.util.Locale;import androID.content.Context;import androID.graphics.color;import androID.graphics.Typeface;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import com.infometricx.goappointed.R;public class Calendaradapter extends BaseAdapter { private Context mContext; private java.util.Calendar month; public GregorianCalendar pmonth; // calendar instance for prevIoUs month /** * calendar instance for prevIoUs month for getting complete vIEw */ public GregorianCalendar pmonthmaxset; private GregorianCalendar selectedDate; int firstDay; int maxWeeknumber; int maxP; int calMaxP; int lastWeekDay; int leftDays; int mnthlength; String itemvalue, curentDateString; DateFormat df; private ArrayList<String> items; public static List<String> dayString; private VIEw prevIoUsVIEw; Typeface mMypriad; public Calendaradapter(Context c, GregorianCalendar monthCalendar) { Calendaradapter.dayString = new ArrayList<String>(); Locale.setDefault(Locale.US); mMypriad = Typeface.createFromAsset(c.getAssets(), "Fonts/MyriadPro-light.otf"); month = monthCalendar; selectedDate = (GregorianCalendar) monthCalendar.clone(); mContext = c; month.set(GregorianCalendar.DAY_OF_MONTH, 1); this.items = new ArrayList<String>(); df = new SimpleDateFormat("yyyy-MM-dd", Locale.US); curentDateString = df.format(selectedDate.getTime()); refreshDays(); } public voID setItems(ArrayList<String> items) { for (int i = 0; i != items.size(); i++) { if (items.get(i).length() == 1) { items.set(i, "0" + items.get(i)); } } this.items = items; } public int getCount() { return dayString.size(); } public Object getItem(int position) { return dayString.get(position); } public long getItemID(int position) { return 0; } // create a new vIEw for each item referenced by the Adapter public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw v = convertVIEw; final TextVIEw dayVIEw; if (convertVIEw == null) { // if it's not recycled, initialize some // attributes LayoutInflater vi = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.calenderitem, null); } dayVIEw = (TextVIEw) v.findVIEwByID(R.ID.date); // separates daystring into parts. String[] separatedTime = dayString.get(position).split("-"); // taking last part of date. IE; 2 from 2012-12-02 String grIDvalue = separatedTime[2].replaceFirst("^0*", ""); // checking whether the day is in current month or not. if ((Integer.parseInt(grIDvalue) > 1) && (position < firstDay)) { // setting offdays to white color. dayVIEw.setTextcolor(color.WHITE); dayVIEw.setClickable(false); dayVIEw.setFocusable(false); } else if ((Integer.parseInt(grIDvalue) < 7) && (position > 28)) { dayVIEw.setTextcolor(color.WHITE); dayVIEw.setClickable(false); dayVIEw.setFocusable(false); } else { // setting curent month's days in blue color. dayVIEw.setTextcolor(color.BLACK); } if (dayString.get(position).equals(curentDateString)) { setSelected(v); prevIoUsVIEw = v; } else { v.setBackgroundResource(R.drawable.List_item_background); } dayVIEw.setText(grIDvalue); dayVIEw.setTypeface(mMypriad); // create date string for comparison String date = dayString.get(position); Log.d("date", "--->" + date); if (date.length() == 1) { date = "0" + date; } String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1); if (monthStr.length() == 1) { monthStr = "0" + monthStr; } return v; } public VIEw setSelected(VIEw vIEw) { if (prevIoUsVIEw != null) { prevIoUsVIEw.setBackgroundResource(R.drawable.List_item_background); } prevIoUsVIEw = vIEw; vIEw.setBackgroundResource(R.drawable.calendar_cel_selectl); return vIEw; } public voID refreshDays() { // clear items items.clear(); dayString.clear(); Locale.setDefault(Locale.US); pmonth = (GregorianCalendar) month.clone(); // month start day. IE; sun, mon, etc firstDay = month.get(GregorianCalendar.DAY_OF_WEEK); // finding number of weeks in current month. maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH); // allocating maximum row number for the grIDvIEw. mnthlength = maxWeeknumber * 7; maxP = getMaxP(); // prevIoUs month maximum day 31,30.... calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ... /** * Calendar instance for getting a complete grIDvIEw including the three * month's (prevIoUs,current,next) dates. */ pmonthmaxset = (GregorianCalendar) pmonth.clone(); /** * setting the start date as prevIoUs month's required date. */ pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1); /** * filling calendar grIDvIEw. */ for (int n = 0; n < mnthlength; n++) { itemvalue = df.format(pmonthmaxset.getTime()); pmonthmaxset.add(GregorianCalendar.DATE, 1); dayString.add(itemvalue); } } private int getMaxP() { int maxP; if (month.get(GregorianCalendar.MONTH) == month .getActualMinimum(GregorianCalendar.MONTH)) { pmonth.set((month.get(GregorianCalendar.YEAR) - 1), month.getActualMaximum(GregorianCalendar.MONTH), 1); } else { pmonth.set(GregorianCalendar.MONTH, month.get(GregorianCalendar.MONTH) - 1); } maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); return maxP; }}相关方法:
dayVIEw = (TextVIEw) v.findVIEwByID(R.ID.date); // separates daystring into parts. String[] separatedTime = dayString.get(position).split("-"); // taking last part of date. IE; 2 from 2012-12-02 String grIDvalue = separatedTime[2].replaceFirst("^0*", ""); // checking whether the day is in current month or not. if ((Integer.parseInt(grIDvalue) > 1) && (position < firstDay)) { // setting offdays to white color. dayVIEw.setTextcolor(color.WHITE); dayVIEw.setClickable(false); dayVIEw.setFocusable(false); } else if ((Integer.parseInt(grIDvalue) < 7) && (position > 28)) { dayVIEw.setTextcolor(color.WHITE); dayVIEw.setClickable(false); dayVIEw.setFocusable(false); } else { // setting curent month's days in blue color. dayVIEw.setTextcolor(color.BLACK); }这是我的适配器类,我已经形成了一个日历视图
帮我解决一下
提前致谢.
解决方法:
dayVIEw = (TextVIEw) v.findVIEwByID(R.ID.date); // separates daystring into parts. String[] separatedTime = dayString.get(position).split("-"); // taking last part of date. IE; 2 from 2012-12-02 String grIDvalue = separatedTime[2].replaceFirst("^0*", ""); // checking whether the day is in current month or not. if ((Integer.parseInt(grIDvalue) > 1) && (position < firstDay)) { // setting offdays to white color. dayVIEw.setTextcolor(color.WHITE); dayVIEw.setClickable(false); dayVIEw.setFocusable(false); float Alpha = 0.55f; AlphaAnimation AlphaUp = new AlphaAnimation(Alpha, Alpha); AlphaUp.setFillAfter(true); dayVIEw.startAnimation(AlphaUp); } else if ((Integer.parseInt(grIDvalue) < 7) && (position > 28)) { dayVIEw.setTextcolor(color.WHITE); dayVIEw.setClickable(false); dayVIEw.setFocusable(false); float Alpha = 0.35f; AlphaAnimation AlphaUp = new AlphaAnimation(Alpha, Alpha); AlphaUp.setFillAfter(true); dayVIEw.startAnimation(AlphaUp); dayVIEw.setClickable(false); } else { // setting curent month's days in blue color. dayVIEw.setTextcolor(color.BLACK); float Alpha = 0.75f; AlphaAnimation AlphaUp = new AlphaAnimation(Alpha, Alpha); AlphaUp.setFillAfter(true); dayVIEw.startAnimation(AlphaUp); dayVIEw.setClickable(false); } 总结 以上是内存溢出为你收集整理的启用和禁用android中的日期全部内容,希望文章能够帮你解决启用和禁用android中的日期所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)