Android Datepicker - 始终显示日期月份微调器布局而不是日历视图

时间:2016-05-26 08:17:30

标签: android

在使用android datepicker时,我想总是显示旧的日期月份微调器而不是新的日历视图?我怎么能这样做?

我希望这适用于运行任何Android版本的所有设备: enter image description here 并且不想要关注ui:

enter image description here

4 个答案:

答案 0 :(得分:9)

我使用这个答案https://stackoverflow.com/a/31610267/5700434来彻底解决问题。

主要使用:

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

完整代码:

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{
            LogUtils.w(TAG, "onDateSetListener callback is not initialized.");
        }
    }
}

答案 1 :(得分:1)

有很多库可供Android日期选择器使用(例如:exmple1 example2 example3。或者您可以使用默认日期选择器。This日期选择器可能是一个你正在寻找

答案 2 :(得分:0)

dislike

这在android的所有版本中都运行良好。从4.4到6.0         经过测试虽然已被弃用,但它得到了很好的支持。

答案 3 :(得分:0)

使用DatePicker代替“日历视图”,并添加以下xml属性:

android:datePickerMode="spinner"
android:calendarViewShown="false"

给出以下结果:

enter image description here