我设置了两个日期选择器(即签入和签出)。我第一次运行应用程序时一切都很好,我点击的日期显示在TextView.But而不运行应用程序第二次当我尝试单击“检入”按钮时,日期在TextView中没有变化。但日期在LogCat中发生了变化。同时Check Out按钮第一次正常工作,第二次正常工作(不再运行应用程序)。请解决我的问题。
这是我的代码:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends ActionBarActivity {
Button checkIn, checkOut, checkavailability;
TextView displayCheckIn, displayCheckOut;
int year, month, day;
static final int DATE_DIALOG_ID = 1;
static final int DATE_DIALOG_ID2 = 2;
int cur = 0;
String chidate, chodate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkIn = (Button) findViewById(R.id.checkInBtn);
checkOut = (Button) findViewById(R.id.checkOutBtn);
displayCheckIn = (TextView) findViewById(R.id.displayCheckIntv);
displayCheckOut = (TextView) findViewById(R.id.displayCheckOuttv);
checkavailability = (Button) findViewById(R.id.checkAvailabilityBtn);
setCurrentDateOnView();//declaring a method to display the current date in the opening
addListenerOnButton();
}
//settin the current date in the opening page
public void setCurrentDateOnView() {
Calendar today = Calendar.getInstance();
year = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);
//displaying current date in the check in textView
displayCheckIn.setText(year + "-" + (month + 1) + "-" + day);
//displaying the current date in check out textView by getting the text from the check in view
displayCheckOut.setText(year + "-" + (month + 1) + "-" + day);
}
public void addListenerOnButton() {
checkIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
checkOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID2);
}
});
checkavailability.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//checkAvailability();
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("onCreate Dialog :" + id);
cur = DATE_DIALOG_ID;
//Displaying the Date Picker Dialog on the screen with today's date
return new DatePickerDialog(this, datePickerListener, year, month, day);
case DATE_DIALOG_ID2:
System.out.println("onCreateDialog :" + id);
cur = DATE_DIALOG_ID2;
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int syear, int monthOfYear, int dayOfMonth) {
year = syear;
month = monthOfYear;
day = dayOfMonth;
if (cur == DATE_DIALOG_ID) {
displayCheckIn.setText(year + "-" + (month + 1) + "-" + day);
chidate = displayCheckIn.getText().toString();
System.out.println(chidate);
//Toast.makeText(getApplicationContext(),chidate,Toast.LENGTH_LONG).show();
} else {
displayCheckOut.setText(year + "-" + (month + 1) + "-" + day);
chodate = displayCheckOut.getText().toString();
System.out.println(chodate);
//Toast.makeText(getApplicationContext(),chodate,Toast.LENGTH_LONG).show();
}
}
};
//这是我每次都要检查Checkin日期的代码。
/*public void checkAvailability() {
String[] datesin = {"2014-12-16", "2014-12-17", "2014-12-18"};
for (int j = 0; j < datesin.length; j++) {
if (datesin[j].equals(chidate)) {
Toast.makeText(getApplicationContext(), "Room Not Available", Toast.LENGTH_LONG).show();
break;
} else
Toast.makeText(getApplicationContext(), "Room Available", Toast.LENGTH_LONG).show();
}
}*/
}
我的XML是:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/displayCheckIntv"
android:text="Hiiiiiiiiiiiiiiiiiiiii"
android:layout_toEndOf="@+id/datePicker"
android:layout_alignTop="@+id/datePicker"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckIn"
android:id="@+id/checkInBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Out"
android:id="@+id/checkOutBtn"
android:layout_marginTop="48dp"
android:layout_below="@+id/checkInBtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Out"
android:id="@+id/displayCheckOuttv"
android:layout_toEndOf="@+id/checkOutBtn"
android:layout_alignBottom="@+id/checkOutBtn"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Availability"
android:id="@+id/checkAvailabilityBtn"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/displayCheckOuttv"
android:layout_alignStart="@+id/displayCheckOuttv" />
</RelativeLayout>