我正在学习android编程并开发了一个小应用程序,其中包括2个日期选择器在同一屏幕上(开始和结束日期)。
它适用于Android 2.3版但在我在Android 4.4版中设置日期时崩溃了。
以下是java代码段。
公共类WriteExcelActivity扩展了Activity {
private TextView startDateDisplay;
private TextView endDateDisplay;
private Button startPickDate;
private Button endPickDate;
private Calendar startDate;
private Calendar endDate;
static final int DATE_DIALOG_ID = 0;
private TextView activeDateDisplay;
private Calendar activeDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_writeexcel);
/* capture our View elements for the start date function */
startDateDisplay = (TextView) findViewById(R.id.fromdate);
startPickDate = (Button) findViewById(R.id.imageButton1);
startDate = Calendar.getInstance();
/* add a click listener to the button */
startPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(startDateDisplay, startDate);
}
});
/* capture our View elements for the end date function */
endDateDisplay = (TextView) findViewById(R.id.todate);
endPickDate = (Button) findViewById(R.id.imageButton2);
/* get the current date */
endDate = Calendar.getInstance();
/* add a click listener to the button */
endPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(endDateDisplay, endDate);
}
});
/* display the current date (this method is below) */
updateDisplay(startDateDisplay, startDate);
updateDisplay(endDateDisplay, endDate);
}
private void updateDisplay(TextView dateDisplay, Calendar date) {
dateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(date.get(Calendar.MONTH) + 1).append("-")
.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.YEAR)).append(" "));
}
public void showDateDialog(TextView dateDisplay, Calendar date) {
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}
private OnDateSetListener dateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDisplay(activeDateDisplay, activeDate);
unregisterDateDisplay();
}
};
private void unregisterDateDisplay() {
activeDateDisplay = null;
activeDate = null;
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
break;
}
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true; }
感谢是否有人可以指出错误。谢谢
===========================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toDate"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textbox_fromdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/txt_textbox_fromdate"
android:layout_marginTop="22dp" />
<TextView
android:id="@+id/fromdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textbox_fromdate"
android:ems="10"
/>
<Button
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/fromdate"
android:layout_toRightOf="@+id/fromdate"
android:drawableLeft="@drawable/ic_datepicker"
/>
<TextView
android:id="@+id/textbox_todate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/txt_textbox_todate"
android:layout_below="@+id/fromdate" />
<TextView
android:id="@+id/todate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textbox_todate"
/>
<Button
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/todate"
android:layout_toRightOf="@+id/todate"
android:drawableLeft="@drawable/ic_datepicker"
android:contentDescription="" />
<Button
android:id="@+id/btnexcel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/todate"
android:onClick="onClickWriteExcelData"
android:text="Write excel data" />
</RelativeLayout>
enter code here
答案 0 :(得分:0)
使用它,它应该有用。
public void onDateSet(android.widget.DatePicker view, int year,int monthOfYear, int dayOfMonth)