0

我有一个使用 DatePickerDialog 设置日期的应用程序。如果您更改微调器上的日期,我希望在小部件上显示日期和星期几。在带有 2.3.3 的三星 mini 上运行时确实会发生这种情况。但是,当在运行 4.0.4 的 htc one x 上编译时,日期和星期几不存在。

无论使用哪个设备或 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);

        }
    };

[编辑2]

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)
4

1 回答 1

0

您可以使用 joda-time-2.0.jar api。这个 api 有 Datetime 类。你现在可以从这门课上得到时间。

DateTime datetime = DateTime.now();
于 2012-11-06T15:19:44.557 回答