欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习教程之日历库使用(15)

程序员文章站 2024-02-29 14:29:04
本教程为大家分享了android日历库的使用方法,供大家参考,具体内容如下 mainactivity.java代码: package siso.weekv;...

本教程为大家分享了android日历库的使用方法,供大家参考,具体内容如下

mainactivity.java代码:

package siso.weekv;

import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;

public class mainactivity extends appcompatactivity {

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  findviewbyid(r.id.buttonbasic).setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    intent intent = new intent(mainactivity.this, basicactivity.class);
    startactivity(intent);
   }
  });

  findviewbyid(r.id.buttonasynchronous).setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    intent intent = new intent(mainactivity.this, asynchronousactivity.class);
    startactivity(intent);
   }
  });
 }

}

asynchronousactivity.java代码:

package siso.weekv;

import android.widget.toast;


import java.util.arraylist;
import java.util.calendar;
import java.util.list;

import retrofit.callback;
import retrofit.restadapter;
import retrofit.retrofiterror;
import retrofit.client.response;
import siso.weekv.apiclient.event;
import siso.weekv.apiclient.myjsonservice;
import siso.wvlib.weekviewevent;


/**
 * an example of how events can be fetched from network and be displayed on the week view.
 * created by raquib-ul-alam kanak on 1/3/2014.
 * website: http://alamkanak.github.io
 */
public class asynchronousactivity extends baseactivity implements callback<list<event>> {

 private list<weekviewevent> events = new arraylist<weekviewevent>();
 boolean callednetwork = false;

 @override
 public list<? extends weekviewevent> onmonthchange(int newyear, int newmonth) {

  // download events from network if it hasn't been done already. to understand how events are
  // downloaded using retrofit, visit http://square.github.io/retrofit
  if (!callednetwork) {
   restadapter retrofit = new restadapter.builder()
     .setendpoint("https://api.myjson.com/bins")
     .build();
   myjsonservice service = retrofit.create(myjsonservice.class);
   service.listevents(this);
   callednetwork = true;
  }

  // return only the events that matches newyear and newmonth.
  list<weekviewevent> matchedevents = new arraylist<weekviewevent>();
  for (weekviewevent event : events) {
   if (eventmatches(event, newyear, newmonth)) {
    matchedevents.add(event);
   }
  }
  return matchedevents;
 }

 /**
  * checks if an event falls into a specific year and month.
  * @param event the event to check for.
  * @param year the year.
  * @param month the month.
  * @return true if the event matches the year and month.
  */
 private boolean eventmatches(weekviewevent event, int year, int month) {
  return (event.getstarttime().get(calendar.year) == year && event.getstarttime().get(calendar.month) == month - 1) || (event.getendtime().get(calendar.year) == year && event.getendtime().get(calendar.month) == month - 1);
 }

 @override
 public void success(list<event> events, response response) {
  this.events.clear();
  for (event event : events) {
   this.events.add(event.toweekviewevent());
  }
  getweekview().notifydatasetchanged();
 }

 @override
 public void failure(retrofiterror error) {
  error.printstacktrace();
  toast.maketext(this, r.string.async_error, toast.length_short).show();
 }
}

baseactivity.java

package siso.weekv;

import android.graphics.rectf;
import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.util.typedvalue;
import android.view.menu;
import android.view.menuitem;
import android.widget.toast;

import java.text.simpledateformat;
import java.util.calendar;
import java.util.locale;

import siso.wvlib.datetimeinterpreter;
import siso.wvlib.monthloader;
import siso.wvlib.weekview;
import siso.wvlib.weekviewevent;

public abstract class baseactivity extends appcompatactivity implements weekview.eventclicklistener, monthloader.monthchangelistener, weekview.eventlongpresslistener, weekview.emptyviewlongpresslistener {
 private static final int type_day_view = 1;
 private static final int type_three_day_view = 2;
 private static final int type_week_view = 3;
 private int mweekviewtype = type_three_day_view;
 private weekview mweekview;


 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_base);

  // get a reference for the week view in the layout.
  mweekview = (weekview) findviewbyid(r.id.weekview);

  // show a toast message about the touched event.
  mweekview.setoneventclicklistener(this);


  mweekview.setmonthchangelistener(this);

  // set long press listener for events.
  mweekview.seteventlongpresslistener(this);


  mweekview.setemptyviewlongpresslistener(this);


  setupdatetimeinterpreter(false);
 }


 @override
 public boolean oncreateoptionsmenu(menu menu) {
  getmenuinflater().inflate(r.menu.main, menu);
  return true;
 }

 @override
 public boolean onoptionsitemselected(menuitem item) {
  int id = item.getitemid();
  setupdatetimeinterpreter(id == r.id.action_week_view);
  switch (id){
   case r.id.action_today:
    mweekview.gototoday();
    return true;
   case r.id.action_day_view:
    if (mweekviewtype != type_day_view) {
     item.setchecked(!item.ischecked());
     mweekviewtype = type_day_view;
     mweekview.setnumberofvisibledays(1);

     // lets change some dimensions to best fit the view.
     mweekview.setcolumngap((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 8, getresources().getdisplaymetrics()));
     mweekview.settextsize((int) typedvalue.applydimension(typedvalue.complex_unit_sp, 12, getresources().getdisplaymetrics()));
     mweekview.seteventtextsize((int) typedvalue.applydimension(typedvalue.complex_unit_sp, 12, getresources().getdisplaymetrics()));
    }
    return true;
   case r.id.action_three_day_view:
    if (mweekviewtype != type_three_day_view) {
     item.setchecked(!item.ischecked());
     mweekviewtype = type_three_day_view;
     mweekview.setnumberofvisibledays(3);

     // lets change some dimensions to best fit the view.
     mweekview.setcolumngap((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 8, getresources().getdisplaymetrics()));
     mweekview.settextsize((int) typedvalue.applydimension(typedvalue.complex_unit_sp, 12, getresources().getdisplaymetrics()));
     mweekview.seteventtextsize((int) typedvalue.applydimension(typedvalue.complex_unit_sp, 12, getresources().getdisplaymetrics()));
    }
    return true;
   case r.id.action_week_view:
    if (mweekviewtype != type_week_view) {
     item.setchecked(!item.ischecked());
     mweekviewtype = type_week_view;
     mweekview.setnumberofvisibledays(7);


     mweekview.setcolumngap((int) typedvalue.applydimension(typedvalue.complex_unit_dip, 2, getresources().getdisplaymetrics()));
     mweekview.settextsize((int) typedvalue.applydimension(typedvalue.complex_unit_sp, 10, getresources().getdisplaymetrics()));
     mweekview.seteventtextsize((int) typedvalue.applydimension(typedvalue.complex_unit_sp, 10, getresources().getdisplaymetrics()));
    }
    return true;
  }

  return super.onoptionsitemselected(item);
 }


 private void setupdatetimeinterpreter(final boolean shortdate) {
  mweekview.setdatetimeinterpreter(new datetimeinterpreter() {
   @override
   public string interpretdate(calendar date) {
    simpledateformat weekdaynameformat = new simpledateformat("eee", locale.getdefault());
    string weekday = weekdaynameformat.format(date.gettime());
    simpledateformat format = new simpledateformat(" m/d", locale.getdefault());


    if (shortdate)
     weekday = string.valueof(weekday.charat(0));
    return weekday.touppercase() + format.format(date.gettime());
   }

   @override
   public string interprettime(int hour) {
    return hour > 11 ? (hour - 12) + " pm" : (hour == 0 ? "12 am" : hour + " am");
   }
  });
 }

 protected string geteventtitle(calendar time) {
  return string.format("事件 %02d:%02d %s/%d", time.get(calendar.hour_of_day), time.get(calendar.minute), time.get(calendar.month)+1, time.get(calendar.day_of_month));
 }

 @override
 public void oneventclick(weekviewevent event, rectf eventrect) {
  toast.maketext(this, "点击 " + event.getname(), toast.length_short).show();
 }

 @override
 public void oneventlongpress(weekviewevent event, rectf eventrect) {
  toast.maketext(this, "长按事件: " + event.getname(), toast.length_short).show();
 }

 @override
 public void onemptyviewlongpress(calendar time) {
  toast.maketext(this, "空视图长按: " + geteventtitle(time), toast.length_short).show();
 }

 public weekview getweekview() {
  return mweekview;
 }
}

basicactivity.java

package siso.weekv;


import java.util.arraylist;
import java.util.calendar;
import java.util.list;

import siso.wvlib.weekviewevent;


public class basicactivity extends baseactivity {

 @override
 public list<? extends weekviewevent> onmonthchange(int newyear, int newmonth) {
  // populate the week view with some events.
  list<weekviewevent> events = new arraylist<weekviewevent>();

  calendar starttime = calendar.getinstance();
  starttime.set(calendar.hour_of_day, 3);
  starttime.set(calendar.minute, 0);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  calendar endtime = (calendar) starttime.clone();
  endtime.add(calendar.hour, 1);
  endtime.set(calendar.month, newmonth-1);
  weekviewevent event = new weekviewevent(1, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_01));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.hour_of_day, 3);
  starttime.set(calendar.minute, 30);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  endtime = (calendar) starttime.clone();
  endtime.set(calendar.hour_of_day, 4);
  endtime.set(calendar.minute, 30);
  endtime.set(calendar.month, newmonth-1);
  event = new weekviewevent(10, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_02));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.hour_of_day, 4);
  starttime.set(calendar.minute, 20);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  endtime = (calendar) starttime.clone();
  endtime.set(calendar.hour_of_day, 5);
  endtime.set(calendar.minute, 0);
  event = new weekviewevent(10, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_03));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.hour_of_day, 5);
  starttime.set(calendar.minute, 30);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  endtime = (calendar) starttime.clone();
  endtime.add(calendar.hour_of_day, 2);
  endtime.set(calendar.month, newmonth-1);
  event = new weekviewevent(2, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_02));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.hour_of_day, 5);
  starttime.set(calendar.minute, 0);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  starttime.add(calendar.date, 1);
  endtime = (calendar) starttime.clone();
  endtime.add(calendar.hour_of_day, 3);
  endtime.set(calendar.month, newmonth - 1);
  event = new weekviewevent(3, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_03));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.day_of_month, 15);
  starttime.set(calendar.hour_of_day, 3);
  starttime.set(calendar.minute, 0);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  endtime = (calendar) starttime.clone();
  endtime.add(calendar.hour_of_day, 3);
  event = new weekviewevent(4, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_04));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.day_of_month, 1);
  starttime.set(calendar.hour_of_day, 3);
  starttime.set(calendar.minute, 0);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  endtime = (calendar) starttime.clone();
  endtime.add(calendar.hour_of_day, 3);
  event = new weekviewevent(5, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_01));
  events.add(event);

  starttime = calendar.getinstance();
  starttime.set(calendar.day_of_month, starttime.getactualmaximum(calendar.day_of_month));
  starttime.set(calendar.hour_of_day, 15);
  starttime.set(calendar.minute, 0);
  starttime.set(calendar.month, newmonth-1);
  starttime.set(calendar.year, newyear);
  endtime = (calendar) starttime.clone();
  endtime.add(calendar.hour_of_day, 3);
  event = new weekviewevent(5, geteventtitle(starttime), starttime, endtime);
  event.setcolor(getresources().getcolor(r.color.event_color_02));
  events.add(event);

  return events;
 }

}

apiclient文件夹下event.java和myjsonservice.java

package siso.weekv.apiclient;

import android.annotation.suppresslint;
import android.graphics.color;


import com.google.gson.annotations.expose;
import com.google.gson.annotations.serializedname;

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.calendar;
import java.util.date;

import siso.wvlib.weekviewevent;


public class event {

 @expose @serializedname("name")
 private string mname;
 @expose @serializedname("dayofmonth")
 private int mdayofmonth;
 @expose @serializedname("starttime")
 private string mstarttime;
 @expose @serializedname("endtime")
 private string mendtime;
 @expose @serializedname("color")
 private string mcolor;

 public string getname() {
  return mname;
 }

 public void setname(string name) {
  this.mname = name;
 }

 public int getdayofmonth() {
  return mdayofmonth;
 }

 public void setdayofmonth(int dayofmonth) {
  this.mdayofmonth = dayofmonth;
 }

 public string getstarttime() {
  return mstarttime;
 }

 public void setstarttime(string starttime) {
  this.mstarttime = starttime;
 }

 public string getendtime() {
  return mendtime;
 }

 public void setendtime(string endtime) {
  this.mendtime = endtime;
 }

 public string getcolor() {
  return mcolor;
 }

 public void setcolor(string color) {
  this.mcolor = color;
 }

 @suppresslint("simpledateformat")
 public weekviewevent toweekviewevent(){

  // parse time.
  simpledateformat sdf = new simpledateformat("hh:mm");
  date start = new date();
  date end = new date();
  try {
   start = sdf.parse(getstarttime());
  } catch (parseexception e) {
   e.printstacktrace();
  }
  try {
   end = sdf.parse(getendtime());
  } catch (parseexception e) {
   e.printstacktrace();
  }

  // initialize start and end time.
  calendar now = calendar.getinstance();
  calendar starttime = (calendar) now.clone();
  starttime.settimeinmillis(start.gettime());
  starttime.set(calendar.year, now.get(calendar.year));
  starttime.set(calendar.month, now.get(calendar.month));
  starttime.set(calendar.day_of_month, getdayofmonth());
  calendar endtime = (calendar) starttime.clone();
  endtime.settimeinmillis(end.gettime());
  endtime.set(calendar.year, starttime.get(calendar.year));
  endtime.set(calendar.month, starttime.get(calendar.month));
  endtime.set(calendar.day_of_month, starttime.get(calendar.day_of_month));

  // create an week view event.
  weekviewevent weekviewevent = new weekviewevent();
  weekviewevent.setname(getname());
  weekviewevent.setstarttime(starttime);
  weekviewevent.setendtime(endtime);
  weekviewevent.setcolor(color.parsecolor(getcolor()));

  return weekviewevent;
 }
}

package siso.weekv.apiclient;

import java.util.list;

import retrofit.callback;
import retrofit.http.get;

/**
 * created by raquib-ul-alam kanak on 1/3/16.
 * website: http://alamkanak.github.io
 */
public interface myjsonservice {

 //https://api.myjson.com/bins/3nh0k
 @get("/3nh0k")
 void listevents(callback<list<event>> eventscallback);

}

app项目结构:

Android学习教程之日历库使用(15)

andriod类库项目结构:

Android学习教程之日历库使用(15)

项目运行结果如图:

Android学习教程之日历库使用(15)

Android学习教程之日历库使用(15)

Android学习教程之日历库使用(15)

Android学习教程之日历库使用(15)

Android学习教程之日历库使用(15)

Android学习教程之日历库使用(15)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。