我有一个使用DatePickerDialog设置日期的应用。如果您更改微调器上的日期,我希望在小部件上显示日期和星期几。在使用2.3.3的三星迷你版上运行时会发生这种情况。但是当在htc上编译一个x运行4.0.4时,日期和星期几不存在。
我如何确保小部件上始终存在星期和星期几,无论哪个设备或Android版本?
我需要覆盖窗口小部件的默认行为,因为它不一致,具体取决于哪个设备或操作系统。感谢
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
Intent i = new Intent(GetRota.this, NfcscannerActivity.class);
i.putExtra("nextRota", formattedSpinnyTime);
i.setAction("NEXT_ROTA");
startActivity(i);
}
};
[编辑]
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
//return new DatePickerDialog(this, datePickerListener, year, month, day);
DatePickerDialog dpd = new DatePickerDialog(nfcscannerapplication, datePickerListener, year, month, day);
dpd.updateDate(year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
DateTime spinnyTime = new DateTime(year, month+1, day, 1, 1);
Log.e(TAG, "spinnyTime = " + spinnyTime);
DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
String formattedSpinnyTime = fmt.print(spinnyTime);
Log.e(TAG, "spinnyTime = " + formattedSpinnyTime);
Log.e(TAG, "year = " + year);
Log.e(TAG, "month = " + month);
Log.e(TAG, "day = " + day);
nfcscannerapplication.setPreviousGlobalDateTime(nfcscannerapplication.getglobalDateTime());
nfcscannerapplication.setGobalDateTime(spinnyTime);
Intent i = new Intent(GetRota.this, NfcscannerActivity.class);
i.putExtra("nextRota", formattedSpinnyTime);
i.setAction("NEXT_ROTA");
startActivity(i);
}
};
[EDIT2]
11-06 16:27:33.320: E/AndroidRuntime(24628): FATAL EXCEPTION: main
11-06 16:27:33.320: E/AndroidRuntime(24628): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.view.ViewRootImpl.setView(ViewRootImpl.java:595)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:406)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:320)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:152)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.app.Dialog.show(Dialog.java:301)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.app.Activity.showDialog(Activity.java:2983)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.app.Activity.showDialog(Activity.java:2933)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.carefreegroup.GetRota.onOptionsItemSelected(GetRota.java:239)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.app.Activity.onMenuItemSelected(Activity.java:2571)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:981)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:495)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:127)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.view.View$PerformClick.run(View.java:14319)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.os.Handler.handleCallback(Handler.java:608)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.os.Looper.loop(Looper.java:156)
11-06 16:27:33.320: E/AndroidRuntime(24628): at android.app.ActivityThread.main(ActivityThread.java:5045)
11-06 16:27:33.320: E/AndroidRuntime(24628): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 16:27:33.320: E/AndroidRuntime(24628): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-06 16:27:33.320: E/AndroidRuntime(24628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-06 16:27:33.320: E/AndroidRuntime(24628): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您可以使用joda-time-2.0.jar api。这个api有Datetime类。你现在可以从这堂课获得时间。
DateTime datetime = DateTime.now();