Package com. threeeyes. util;
Import java. util. Calendar;
Import com. threeeyes. activity. R;
Import android. R. color;
Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. Gravity;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. ImageButton;
Import android. widget. LinearLayout;
Import android. widget. TableLayout;
Import android. widget. TableRow;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. LinearLayout. LayoutParams;
Public class DateTime extends LinearLayout {
TableLayout table;
TextView showDateTxt;
TextView btnpre;
TextView btnnext;
TextView nowDate;
Int year;
Int month; // 0, 1,..., 11
Int srcday; // The initial day.
Int srcyear; // the initial year.
Int srcmonth; // initial month
String [] weeks = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};
View preSelectedView = null; // The previous selected date
Context ct;
DateCallBack callBack;
Int weekTitleColor = 0xFFFF0000;
Int dayColor = 0xFFFF0000;
Int titleColor = Color. BLACK;
Int selectedColor = Color. TRANSPARENT;
Boolean init = false; // initialization flag
Int colWidth = 30; // cell width
Int rowHeight = 0; // cell height
Int textSize = 12;
LinearLayout dayLayOut;
Public DateTime (Context context, final int year1, final int month1, int day1 ){
Super (context );
Ct = context;
This. srcmonth = month1;
This. srcyear = year1;
SetOrientation (LinearLayout. VERTICAL );
SetLayoutParams (new LayoutParams (
LinearLayout. LayoutParams. WRAP_CONTENT,
LinearLayout. LayoutParams. WRAP_CONTENT ));
SetBackgroundColor (Color. GRAY );
LinearLayout titleLayOut = new LinearLayout (ct );
TitleLayOut. setOrientation (LinearLayout. HORIZONTAL );
TitleLayOut. setGravity (Gravity. CENTER_HORIZONTAL );
AddView (titleLayOut );
Table = new TableLayout (ct );
AddView (table );
ShowDateTxt = new TextView (ct );
LinearLayout. LayoutParams la = new LinearLayout. LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT );
La. rightMargin = 20;
La. leftMargin = 20;
ShowDateTxt. setLayoutParams (la );
ShowDateTxt. setTextSize (textSize );
This. year = year1;
If (month1 <1 | month1> 12)
This. month = 0;
Else
This. month = month1-1;
If (day1 <1 | day1> 31)
This. srcday = 1;
Else
This. srcday = day1;
ShowDateTxt. setText (String. valueOf (year1) + "year" + String. valueOf (month1 ));
ShowDateTxt. setTextColor (titleColor );
Btnpre = new TextView (ct );
Btnpre. setTextColor (titleColor );
Btnpre. setText ("last month ");
Btnpre. setTextSize (textSize );
Btnnext = new TextView (ct );
Btnnext. setTextColor (titleColor );
Btnnext. setText ("Next Month ");
Btnnext. setTextSize (textSize );
NowDate = new TextView (ct );
NowDate. setTextColor (titleColor );
NowDate. setText ("today ");
NowDate. setTextSize (textSize );
NowDate. setGravity (Gravity. CENTER_HORIZONTAL );
DayLayOut = new LinearLayout (ct );
DayLayOut. setOrientation (LinearLayout. VERTICAL );
DayLayOut. setGravity (Gravity. CENTER_VERTICAL );
DayLayOut. addView (showDateTxt );
DayLayOut. addView (nowDate );
TitleLayOut. addView (btnpre );
TitleLayOut. addView (dayLayOut );
TitleLayOut. addView (btnnext );
NowDate. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Year = srcyear;
Month = srcmonth-1;
ShowDateTxt. setText (String. valueOf (year) + "year"
+ String. valueOf (month + 1) + "month ");
LoadDate (1, 1 + 5 );
}
});
Btnnext. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (preSelectedView! = Null)
PreSelectedView. setBackgroundColor (color. transparent );
NextMonth ();
ShowDateTxt. setText (String. valueOf (year) + "year"
+ String. valueOf (month + 1) + "month ");
LoadDate (1, 1 + 5 );
}
});
Btnpre. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
PreMonth ();
ShowDateTxt. setText (String. valueOf (year) + "year"
+ String. valueOf (month + 1) + "month ");
LoadDate (1, 1 + 5 );
}
});
}
Public void setPre_Next_BackGround (int preRes, int nextPre ){
Btnpre. setBackgroundResource (preRes );
Btnnext. setBackgroundResource (nextPre );
Btnpre. setText ("");
Btnnext. setText ("");
}
Public void setNowDateBackGround (int res ){
DayLayOut. setBackgroundResource (res );
}
/**
* Initialization date
*
* @ Param titleCoclor
* Title color
* @ Param weekTitleColor
* Week color
* @ Param dayColor
* Date color
*/
Public void initDate (int titleCoclor, int weekTitleColor, int dayColor,
Int selectedColor ){
If (! Init ){
This. weekTitleColor = weekTitleColor;
This. dayColor = dayColor;
This. titleColor = titleCoclor;
ShowDateTxt. setTextColor (titleCoclor );
Btnpre. setTextColor (titleCoclor );
Btnnext. setTextColor (titleCoclor );
This. selectedColor = selectedColor;
GenerateDate ();
}
}
/**
* Default initialization date color
*/
Public void initDate (){
If (! Init ){
GenerateDate ();
}
}
/**
* The sequence is "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", and "Saturday ".
*
* @ Param weekdays
*/
Public void setWeekTitle (String [] weekdays ){
If (weekdays! = Null & weekdays. length> 0 & weekdays. length = 7)
This. weeks = weekdays;
}
Public int maxDay (){
Calendar time = Calendar. getInstance ();
Log. v ("", time. toString ());
Time. clear ();
Time. set (Calendar. YEAR, year );
Time. set (Calendar. MONTH, month );
Int day = time. getActualMaximum (Calendar. DAY_OF_MONTH); // number of days of the current month
Return day;
}
Public int nowWeekDay (){
Calendar time = Calendar. getInstance ();
Log. v ("", time. toString ());
Time. clear ();
Time. set (Calendar. YEAR, year );
Time. set (Calendar. MONTH, month );
Time. set (Calendar. DATE, 1 );
Int weekday = time. get (Calendar. DAY_OF_WEEK );
If (weekday = 7)
Return 0;
Else
Return weekday-1;
}
Public void generateDate (){
TableLayout. LayoutParams params = new TableLayout. LayoutParams (
TableLayout. LayoutParams. WRAP_CONTENT,
TableLayout. LayoutParams. WRAP_CONTENT );
TableRow row2 = new TableRow (ct );
Row2.setPadding (0, 2, 0, 0 );
Row2.setGravity (Gravity. CENTER_HORIZONTAL );
For (int I = 0; I <7; I ++ ){
TextView col1 = new TextView (ct );
Col1.setMinWidth (colWidth );
Col1.setMaxWidth (colWidth );
If (rowHeight> 0)
Col1.setMinHeight (rowHeight );
Col1.setTextColor (weekTitleColor );
Col1.setText (weeks [I]);
Col1.setGravity (Gravity. CENTER_HORIZONTAL | Gravity. CENTER_VERTICAL );
Col1.setTextSize (textSize );
Row2.addView (col1); // Add a column
}
Table. addView (row2 );
Int weekday = nowWeekDay ();
Int maxday = maxDay ();
Log. v ("date ----", weekday + "-----------" + maxday );
Int count = 0;
For (int I = 0; I <5; I ++) {// Add 6 rows
TableRow row = new TableRow (ct );
Row. setPadding (0, 2, 0, 2 );
Row. setGravity (Gravity. CENTER_HORIZONTAL );
Row. setLayoutParams (params );
// Row. setBackgroundColor (0xFF00FF00 );
For (int j = 0; j <7; j ++) {// Add 1 column
TextView col = new TextView (ct );
Col. setTextColor (dayColor );
Col. setBackgroundColor (color. transparent );
Col. setTextSize (textSize );
If (rowHeight> 0)
Col. setMinHeight (rowHeight );
If (I = 0 ){
If (weekday <= j ){
Count ++;
Col. setText (String. valueOf (count ));
}
} Else {
If (count <maxday ){
Count ++;
Col. setText (String. valueOf (count ));
}
}
Col. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (TextView) v). getText (). toString (). length ()> 0 ){
If (preSelectedView! = Null ){
PreSelectedView
. SetBackgroundColor (color. transparent );
}
PreSelectedView = v;
V. setBackgroundColor (selectedColor );
CallBack.exe cute (v, year + "", (month + 1) + "",
(TextView) v). getText (). toString ());
}
}
});
If (srcday = count ){
Col. setBackgroundColor (selectedColor );
This. preSelectedView = col;
}
Col. setGravity (Gravity. CENTER_HORIZONTAL
| Gravity. CENTER_VERTICAL );
Row. addView (col); // Add a column
}
Table. addView (row); // Add rows
}
}
Public void nextMonth (){
If (month = 11 ){
Year ++;
Month = 0;
} Else {
Month ++;
}
}
Public void preMonth (){
If (month = 0 ){
Year --;
Month = 11;
} Else {
Month --;
}
}
Public void loadDate (int startIndex, int endIndex ){
Int weekday = nowWeekDay ();
Int maxday = maxDay ();
Log. v ("date ----", weekday + "-----------" + maxday );
Int count = 0;
For (int I = startIndex; I <endIndex; I ++ ){
TableRow row = (TableRow) table. getChildAt (I );
For (int j = 0; j <7; j ++ ){
TextView col = (TextView) row. getChildAt (j );
If (I = startIndex ){
If (weekday <= j ){
Count ++;
Col. setText (String. valueOf (count ));
If (srcday = count ){
Col. setBackgroundColor (selectedColor );
This. preSelectedView = col;
}
} Else {
Col. setText ("");
}
} Else {
If (count <maxday ){
Count ++;
Col. setText (String. valueOf (count ));
} Else {
Col. setText ("");
}
}
If (srcday = count ){
Col. setBackgroundColor (selectedColor );
If (this. preSelectedView! = Col)
This. preSelectedView
. SetBackgroundColor (color. transparent );
This. preSelectedView = col;
}
}
}
}
/**
* Callback function
*
* @ Author Acer
*
*/
Public interface DateCallBack {
Public void execute (View v, String year, String month, String day );
}
Public void setCallBack (DateCallBack callBack ){
This. callBack = callBack;
}
/**
* Set the cell width and height.
*
* @ Param colWidth
* Cell width
* @ Param rowHeight
* Cell height
* @ Param textSize1
* Text Size
*/
Public void setWidthHeightTextSize (int colWidth, int rowHeight,
Int textSize1 ){
If (colWidth> 0)
This. colWidth = colWidth;
This. rowHeight = rowHeight;
This. textSize = textSize1;
Btnpre. setTextSize (textSize1 );
Btnnext. setTextSize (textSize1 );
ShowDateTxt. setTextSize (textSize1 );
}
}
From ygc974257893