简体   繁体   中英

Samsung Galaxy S7 Edge ignores datePickerMode

I am setting the datePickerMode in my styles file to spinner like this:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:dialogTheme">@style/MyDialogTheme</item>
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>

<style name="ItvDialogTheme" parent="android:Theme.Material.Light.Dialog">
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>

<style name="MyDatePicker" parent="android:Widget.Material.Light.DatePicker">
    <item name="android:datePickerMode">spinner</item>
</style>

and then create a DatePicker

DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
        //handle result
        }
    };

DatePickerDialog dialog = new DatePickerDialog(this, onDateSetListener, dob.getYear(), dob.getMonth() - 1,
            dob.getDay());

dialog.show();

This worked fine across a lot of devices including the Samsung Galaxy S7 Edge on Android 6, but since updating the device to Android 7 it ignores the mode spinner and shows the calendar.

The spinner works fine on an One Plus 3 on Android 7.1.1.
Any ideas?

You may be running into this issue (#37120178) datepicker dialog can't switch to spinner on Android 7.0 device . If so, there is a work-around documented on that page. I haven't tried the work-around, but it may be worth a shot. (See below for implementation of the work-around.)

By the way, I have a Samsung S7 (not an edge) running 7.0. I can get a DatePicker spinner if I define it in XML, so another work-around would be to define the DatePicker spinner in XML and use it in the dialog.

Here is a gist of this solution .


Odd, but those fixes in the bug report seem to be for the time picker although the report is for the date picker. I dug around a bit and found Use DatePicker spinners in API 24 as a potential fix. Again, I have not tried this.

This reflection logic does appear to work. It showed the spinner beside the calendar view for me. If you want just the spinner, you will need to add the following to your style:

<item name="android:calendarViewShown">false</item>

(Credit for this work-around goes to lognaturel and jeffdgr8 ).

Here is what my custom DatePickerDialog looks like with the reflection logic:

public class MyReflectionDatePickerDialog extends DatePickerDialog {

    private DatePicker mDatePicker;

    @RequiresApi(api = Build.VERSION_CODES.N)
    public MyReflectionDatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener,
                                        int year, int month, int dayOfMonth) {
        super(context, listener, year, month, dayOfMonth);
        fixSpinner(context, year, month, dayOfMonth);

    }

    // include gist here from:
    // https://gist.github.com/lognaturel/232395ee1079ff9e4b1b8e7096c3afaf
}

You would instantiate this custom view as shown below:

        DatePickerDialog dialog;
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
            dialog = new MyDatePickerDialog(this, onDateSetListener, dob.getYear(), 
                dob.getMonth() - 1, dob.getDay());
        } else {
            dialog = new DatePickerDialog(this, onDateSetListener, dob.getYear(), 
                dob.getMonth() - 1, dob.getDay());
        }

You can try this.

datePicker.setCalendarViewShown(false);

由于Android 7.0,您应该尝试将targetSdkVersion提升至24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM