calendarViewShown在Android 5.0 Lollipop上没有被尊重

时间:2014-11-30 10:30:52

标签: android datepicker android-5.0-lollipop

我正在研究The Big Nerd Ranch的Android指南。在里面 赋值犯罪意图我使用DatePicker跟随 布局。

在5.0之前的设备上,日历未在DatePicker中显示,如中所定义 布局,但Android 5.0忽略此并显示日历。任何的想法 可能会发生什么?

完整代码可在Github上找到。

dialog_date.xml

<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_date_datePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:calendarViewShown="false" >
</DatePicker>

DatePickerFragment.java

package com.sudhirkhanger.android.criminalintent;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

public class DatePickerFragment extends DialogFragment {

    public static final String EXTRA_DATE = "com.sudhirkhanger.android.criminalintent.date";
    private Date mDate;

    public static DatePickerFragment newInstance(Date date) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_DATE, date);
        DatePickerFragment fragment = new DatePickerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void sendResult(int resultCode) {
        if (getTargetFragment() == null)
            return;
        Intent i = new Intent();
        i.putExtra(EXTRA_DATE, mDate);
        getTargetFragment().onActivityResult(getTargetRequestCode(),
                resultCode, i);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
        // Create a Calendar to get the year, month, and day
        final Calendar calendar = Calendar.getInstance();
        // calendar.setTime(mDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        View v = getActivity().getLayoutInflater().inflate(
                R.layout.dialog_date, null);

        DatePicker datePicker = (DatePicker) v
                .findViewById(R.id.dialog_date_datePicker);
        datePicker.init(year, month, day, new OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year, int month,
                    int day) {
                // Translate year, month, day into a Date object using a
                // calendar
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                mDate = new GregorianCalendar(year, month, day, hour, minute)
                        .getTime();
                // Update argument to preserve selected value on rotation
                getArguments().putSerializable(EXTRA_DATE, mDate);
            }
        });

        return new AlertDialog.Builder(getActivity())
                .setView(v)
                .setTitle(R.string.date_picker_title)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                sendResult(Activity.RESULT_OK);
                            }
                        }).create();

    }
}

2 个答案:

答案 0 :(得分:30)

该解决方案已发布here

您必须同时使用android:calendarViewShown="false"android:datePickerMode="spinner"

这解决了它。

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_date_datePicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:calendarViewShown="false"
    android:datePickerMode="spinner" >
</DatePicker>

答案 1 :(得分:2)

以下代码为我工作

new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Dialog, this, year,month, day);