DateTimePicker 运行 不同取决于 Android 中的 api 级别

DateTimePicker run different depends on api level in Android

我想在我的项目中使用日期时间选择器。

http://www.parallelcodes.com/android-datetime-picker-example/

本教程非常有效。当我 运行 这个 datetimepicker 在 api 19 显示像这个网站,我想这样做。

当我运行 api 22个datetimepicker出现时是这样的

那么,有什么建议吗?

我用这段代码解决了这个问题,

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import java.util.Calendar;


public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private Calendar calendar;
    private Calendar maxCalendar;
    private Calendar minCalendar;
    private DatePickerDialog.OnDateSetListener onDateSetListener;

    private static final String TAG = DatePickerFragment.class.getSimpleName();

    public void setup(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener){
        this.calendar = calendar;
        this.onDateSetListener = onDateSetListener;
    }

    public void setup(Calendar calendar, Calendar maxCalendar, DatePickerDialog.OnDateSetListener onDateSetListener){
        this.calendar = calendar;
        this.maxCalendar = maxCalendar;
        this.onDateSetListener = onDateSetListener;
    }

    public void setup(Calendar calendar, Calendar maxCalendar, Calendar minCalendar){
        this.calendar = calendar;
        this.maxCalendar = maxCalendar;
        this.minCalendar = minCalendar;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        if(minCalendar!=null){
            dialog.getDatePicker().setMinDate(minCalendar.getTimeInMillis());
        }

        if(maxCalendar != null){
            dialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis());
        }

        return dialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        if(onDateSetListener != null){
            onDateSetListener.onDateSet(view, year, month, day);
        }else{
            Log.w(TAG, "onDateSetListener callback is not initialized.");
        }
    }
}

然后这样调用,

   public void showDatePicker() {


            Calendar calendar = Calendar.getInstance();

            DatePickerFragment fragment = new DatePickerFragment();
            fragment.setup(calendar, new OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                }
            });
            fragment.show(getSupportFragmentManager(), null);

    }

这是android自己的日历,设计因不同API而改变。如果您想为所有 API 版本使用相同的日历,那么您应该尝试使用自定义日历而不是默认日历

如果还有什么不明白的请告诉我