如何在点击下个月或上个月的月份开始显示日期?假设本周开始的18月8日,当我点击下个月9月它再次显示日期表格18向前不显示表格开始月份看到这个图像http://imgur.com/27YDBWD 当我点击下一步再次表格18不显示表格1月
public class HoyahCalendar extends Activity implements
View.OnClickListener {
public static int mYear;
public static int currentIndex = -1;
public static int mMonth;
public static int mDay;
public static String[][] a = new String[6][7];
String January="January";
String February="February";
String March="March";
String April="April";
String May="May";
String June="June";
String Jully="Jully";
String August="August";
String September="September";
String October="October";
String November="November";
String December="December";
String Monthname;
TextView date_today;
ImageView last_month;
ImageView next_month;
ImageView last_week;
ImageView next_week;
Button e00;
Button e01;
Button e02;
Button e03;
Button e04;
Button e05;
Button e06;
Button button;
String value ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getIntent().setAction("Already created");
date_today = (TextView) findViewById(R.id.date_today);
last_month = (ImageView) findViewById(R.id.last_month);
next_month = (ImageView) findViewById(R.id.next_month);
last_week = (ImageView) findViewById(R.id.last_week);
next_week = (ImageView) findViewById(R.id.next_week);
e00 = (Button) findViewById(R.id.e00);
e01 = (Button) findViewById(R.id.e01);
e02 = (Button) findViewById(R.id.e02);
e03 = (Button) findViewById(R.id.e03);
e04 = (Button) findViewById(R.id.e04);
e05 = (Button) findViewById(R.id.e05);
e06 = (Button) findViewById(R.id.e06);
e00.setOnClickListener(this);
e01.setOnClickListener(this);
e02.setOnClickListener(this);
e03.setOnClickListener(this);
e04.setOnClickListener(this);
e05.setOnClickListener(this);
e05.setOnClickListener(this);
button = (Button) findViewById(R.id.button);
Calendar mCalendar = Calendar.getInstance();
mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH) + 1;
mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
value =String.valueOf(mDay);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
AlertDialog alertDialog = new
AlertDialog.Builder(HoyahCalendar.this).create(); //Read Update
alertDialog.setMessage("Date is"+ value);
alertDialog.setButton("Continue..", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
last_month.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mMonth == 1) {
mYear -= 1;
mMonth = 12;
new ShowCalendar(mYear, mMonth);
showOnScreen();
} else {
mMonth -= 1;
new ShowCalendar(mYear, mMonth);
showOnScreen();
}
}
});
next_month.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mMonth == 12) {
mYear += 1;
mMonth = 1;
new ShowCalendar(mYear, mMonth);
showOnScreen();
} else {
mMonth += 1;
new ShowCalendar(mYear, mMonth);
showOnScreen();
}
}
});
last_week.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mMonth == 1) {
mYear -= 1;
mMonth = 12;
new ShowCalendar(mYear, mMonth, mDay, "last");
showOnScreen();
} else {
// mMonth -= 1;
new ShowCalendar(mYear, mMonth, mDay, "last");
showOnScreen();
}
}
});
next_week.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mMonth == 12) {
mYear += 1;
mMonth = 1;
new ShowCalendar(mYear, mMonth, mDay, "next");
showOnScreen();
} else {
if (HoyahCalendar.currentIndex == 4) {
HoyahCalendar.currentIndex = 4;
// mMonth += 1;
}
new ShowCalendar(mYear, mMonth, mDay, "next");
showOnScreen();
}
}
});
new ShowCalendar(mYear, mMonth);
showOnScreen();
}
public void showOnScreen() {
if (mMonth ==1)
{
Monthname="January";
}
else
if (mMonth ==2) {
Monthname="February";
}
else
if (mMonth ==3) { Monthname="March";}
else
if (mMonth ==4) { Monthname="April"; }
else
if (mMonth ==5) { Monthname="May";}
else
if (mMonth ==6) { Monthname="June"; }
else
if (mMonth ==7) { Monthname="July";}
else
if (mMonth ==8) { Monthname="August"; }
else
if (mMonth ==9) { Monthname="September";}
else
if (mMonth ==10) { Monthname="October"; }
if (mMonth ==11) { Monthname="November";}
else
if (mMonth ==12) { Monthname="December"; }
date_today.setText( Monthname + " " +mYear);
e00.setText("" + a[0][0]);
if(e00.getText().toString().equals(String.valueOf(mDay)))
{e00.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button1 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e00.setTextColor(Color.parseColor("#000000"));
}
e01.setText("" + a[0][1]);
// if(e01.getText().toString().equalsIgnoreCase (String.valueOf(mDay)))
if(e01.getText().toString().equals(String.valueOf(mDay)))
{
e01.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button2 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e01.setTextColor(Color.parseColor("#000000"));
}
e02.setText("" + a[0][2]);
if(e02.getText().toString().equals(String.valueOf(mDay)))
{e02.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button3 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e02.setTextColor(Color.parseColor("#000000"));
}
e03.setText("" + a[0][3]);
if(e03.getText().toString().equals(String.valueOf(mDay)))
{e03.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button4 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e03.setTextColor(Color.parseColor("#000000"));
}
e04.setText("" + a[0][4]);
if(e04.getText().toString().equals(String.valueOf(mDay)))
{e04.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button5 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e04.setTextColor(Color.parseColor("#000000"));
}
e05.setText("" + a[0][5]);
if(e05.getText().toString().equals(String.valueOf(mDay)))
{e05.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button6 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e05.setTextColor(Color.parseColor("#000000"));
}
e06.setText("" + a[0][6]);
if(e06.getText().toString().equals(String.valueOf(mDay)))
{e06.setTextColor(Color.parseColor("#FFBBFF"));
Toast.makeText(this, "Button7 text equals!", Toast.LENGTH_SHORT).show();
}
else
{
e06.setTextColor(Color.parseColor("#000000"));
}
}
public void onRestart() {
super.onRestart();
Intent intent = getIntent();
finish();
startActivity(intent);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.e00:
// do stuff;
value=e00.getText().toString();
break;
case R.id.e01:
// do stuff;
value=e01.getText().toString();
break;
case R.id.e02:
// do stuff;
value=e02.getText().toString();
break;
case R.id.e03:
// do stuff;
value=e03.getText().toString();
break;
case R.id.e04:
// do stuff;
value=e04.getText().toString();
break;
case R.id.e05:
// do stuff;
value=e05.getText().toString();
break;
case R.id.e06:
// do stuff;
value=e06.getText().toString();
break;
}
}
}
public class ShowCalendar {
int mYear;
int mMonth;
int mDay;
public ShowCalendar(int mYear, int mMonth){
this.mYear = mYear;
this.mMonth = mMonth;
calculateMonthFirstday();
}
public int getmDay() {
return mDay;
}
public void setmDay(int mDay) {
this.mDay = mDay;
}
public ShowCalendar(int mYear, int mMonth, int mDay, String time){
this.mYear = mYear;
this.mMonth = mMonth;
if (time == "next"){
HoyahCalendar.currentIndex++;
if (HoyahCalendar.currentIndex == 5){
HoyahCalendar.currentIndex--;
}
this.mDay = mDay + 7;
} else if (time == "last"){
HoyahCalendar.currentIndex--;
if (HoyahCalendar.currentIndex == -1){
HoyahCalendar.currentIndex++;
}
this.mDay = mDay - 7;
}
calculateMonthFirstday();
}
public void calculateMonthFirstday(){
int month, first_day=0;
if((mYear%4==0 && mYear%100!=0)||(mYear%400==0))
month=1;
else
month=0;
int y, y12, c, c12, m, d;
y = mYear%100;
y12 = (mYear-1)%100; //only for January and February
c = mYear/100;
c12 = (mYear-1)/100;
m = mMonth;
d = 1;
switch(mMonth){
case 1: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(13 + 1)/10 + d - 1;break;}
case 2: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(14 + 1)/10 + d - 1;break;}
case 4: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 5: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 6: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 7: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 8: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 9: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 10: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 11: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 12: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
}
if(first_day<0)
first_day = 7 - (Math.abs(first_day))%7;//first_day每月第一天星期几
else
first_day = first_day%7;
switch(mMonth){
case 1: {CalculateCalendar(1,first_day,31);break;}
case 2: {CalculateCalendar(2,first_day,28+month);break;}
case 3: {CalculateCalendar(3,first_day,31);break;}
case 4: {CalculateCalendar(4,first_day,30);break;}
case 5: {CalculateCalendar(5,first_day,31);break;}
case 6: {CalculateCalendar(6,first_day,30);break;}
case 7: {CalculateCalendar(7,first_day,31);break;}
case 8: {CalculateCalendar(8,first_day,31);break;}
case 9: {CalculateCalendar(9,first_day,30);break;}
case 10:{CalculateCalendar(10,first_day,31);break;}
case 11:{CalculateCalendar(11,first_day,30);break;}
case 12:{CalculateCalendar(12,first_day,31);break;}
}
}
public void CalculateCalendar(int month_no, int week_no, int month_days){
int i, s, targetRow = 0;
int currentDay;
if (this.mDay == 0){
currentDay= HoyahCalendar.mDay;
}else {
currentDay = this.mDay;
}
//String[][] a = new String[6][7];
for (i=0;i<week_no;i++)
HoyahCalendar.a[i/7][i%7] = "";
for(i=week_no; i<week_no + month_days; i++){
s = i - week_no + 1;
HoyahCalendar.a[i/7][i%7] = String.valueOf(s);
if (s == currentDay && HoyahCalendar.currentIndex == -1){
HoyahCalendar.currentIndex = i/7;
}
}
for (i=0; i<7;i++){
if (HoyahCalendar.a[HoyahCalendar.currentIndex][i] == null){
HoyahCalendar.a[0][i] = "";
}else{
HoyahCalendar.a[0][i] =
HoyahCalendar.a[HoyahCalendar.currentIndex][i];
}
}
for(i=week_no+month_days; i<42; i++)
HoyahCalendar.a[i/7][i%7] = "";
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lightgray"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:background="@drawable/topbar"
android:gravity="left|top"
android:height="60sp"
android:orientation="horizontal" >
<Button
android:id="@+id/last_week22"
android:layout_width="54sp"
android:layout_height="60sp"
android:background="@drawable/meenu" />
<ImageView
android:id="@+id/last_month"
android:layout_width="20sp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginLeft="10sp"
android:src="@drawable/calendar_left_arrow_selector" >
</ImageView>
<TextView
android:id="@+id/date_today"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_weight="0.6"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</TextView>
<ImageView
android:id="@+id/next_month"
android:layout_width="20sp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginRight="10sp"
android:src="@drawable/calendar_right_arrow_selector" >
</ImageView>
<Button
android:id="@+id/next_week22"
android:layout_width="54sp"
android:layout_height="60sp"
android:background="@drawable/plus" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/last_week"
android:layout_width="40sp"
android:layout_height="fill_parent"
android:background="@drawable/topbar"
android:src="@drawable/calendar_left_arrow_selector" >
</ImageView>
<TableLayout
android:id="@+id/table_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:stretchColumns="0,1,2,3,4,5,6">
<TableRow
android:paddingBottom="12dp"
android:background="#FEE5AC"
>
<TextView
android:id="@+id/sun"
android:gravity="center"
android:text="@string/sunday"
android:textColor="#000000"/>
<TextView
android:id="@+id/mon"
android:gravity="center"
android:text="@string/monday"
android:textColor="#000000"/>
<TextView
android:id="@+id/tue"
android:gravity="center"
android:text="@string/tuesday"
android:textColor="#000000"/>
<TextView
android:id="@+id/wed"
android:gravity="center"
android:text="@string/wednesday"
android:textColor="#000000"/>
<TextView
android:id="@+id/thur"
android:gravity="center"
android:text="@string/thursday"
android:textColor="#000000"/>
<TextView
android:id="@+id/fri"
android:gravity="center"
android:text="@string/friday"
android:textColor="#000000"/>
<TextView
android:id="@+id/sat"
android:gravity="center"
android:text="@string/saturday"
android:textColor="#000000"/>
</TableRow>
<TableRow
android:paddingBottom="12dp">
<Button
android:id="@+id/e00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
<Button
android:id="@+id/e01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
<Button
android:id="@+id/e02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
>
</Button>
<Button
android:id="@+id/e03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
<Button
android:id="@+id/e04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
<Button
android:id="@+id/e05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
<Button
android:id="@+id/e06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" >
</Button>
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/next_week"
android:layout_width="40sp"
android:layout_height="fill_parent"
android:background="@drawable/topbar"
android:src="@drawable/calendar_right_arrow_selector" >
</ImageView>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:onClick="showAlert"/>
</LinearLayout>
答案 0 :(得分:0)
什么是HoyahCalendar?
直观地调试发生的事情是你正在构建新的ShowCalendar(年,月),然后他们调用calculateMonthFirstDay(),但是什么时候,他们计算mDay并把mDAy = HoyahCalendar.mDay,我不知道这是什么。显然,如果this.mDay == 0是因为点击了下一个/上个月,然后是this.mDay = 1,但是没有时刻更新日期参考。
尝试评论或重命名变量以使帮助更容易。