Android DatePickerDialog Api 23 (Android 6)

Android DatePickerDialog Api 23 (Android 6)

我正在触发一个 DatePickerDialog,在 api 22 (Android 5.1) 之前它一直在工作并且显示良好,我在上面设置混合和最大日期(最小 = 当前日期,最大 = 1从当前日期开始的月份),但它只是在 Api 23 中显示当前日期,我附上了代码和图像。

///////////////////////////////datepickerdialog///////////////////////////////
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        /*
            Create a DatePickerDialog using Theme.

                DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
                    int year, int monthOfYear, int dayOfMonth)
         */

        // DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
        DatePickerDialog dpd = new DatePickerDialog(getActivity(),this,year,month,day);

        if (Build.VERSION.SDK_INT < 22){

            dpd.getDatePicker().setCalendarViewShown(true);
            dpd.getDatePicker().setSpinnersShown(false);
            dpd.getDatePicker().getCalendarView().setShowWeekNumber(false);

        }

        calendar.setTimeInMillis(calendar.getTimeInMillis());
        long mindate = calendar.getTime().getTime();
        calendar.add(Calendar.MONTH, 1);
        long maxdate = calendar.getTime().getTime();

        dpd.getDatePicker().setMinDate(mindate);
        dpd.getDatePicker().setMaxDate(maxdate);

        dpd.getDatePicker().setMinDate(mindate);

        if(Build.VERSION.SDK_INT < 23){

            dpd.setTitle("");

        }

        // Return the DatePickerDialog
        return  dpd;
    }

    @SuppressLint("SimpleDateFormat")
    public void onDateSet(DatePicker view, int year, int month, int day){
        // Do something with the chosen date

        month++;

        evento_fecha = year+"-"+month+"-"+day;          


        month--;

        datetime.set(Calendar.YEAR, year);
        datetime.set(Calendar.MONTH, month);
        datetime.set(Calendar.DAY_OF_MONTH, day);

        SimpleDateFormat mSDF = new SimpleDateFormat("dd/MM/yyyy");
        fecha = mSDF.format(datetime.getTime());

        //fecha_seleccionada.setText(dia+"/"+mes+"/"+year);
        fecha_seleccionada.setText(fecha);

    }
} 

答案 1:结果

在您的 DatePicker 中,您已将 MinDate 设置为 currentTimeMillis(),在您的 MaxDate 中,您已将 1 MONTH 添加到 Calendar

只需简单地使用这段代码。

public class MainActivity extends Activity {

    int mYear, mMonth, mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        final TextView textView = (TextView)findViewById(R.id.textview_totalStone);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int day) {
                                c.set(year, month, day);
                                String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
                                textView.setText(date);

                                mYear = c.get(Calendar.YEAR);
                                mMonth = c.get(Calendar.MONTH);
                                mDay = c.get(Calendar.DAY_OF_MONTH);
                            }
                        }, mYear, mMonth, mDay);
                dpd.getDatePicker().setMinDate(System.currentTimeMillis());

                Calendar d = Calendar.getInstance();
                d.add(Calendar.MONTH,1);

                dpd.getDatePicker().setMaxDate(d.getTimeInMillis());
                dpd.show();


            }

        });

    }

在我的 DatePicker 中我有日期 June 25 和它 select 并且根据你的要求你有 MaxDate 1 个月它显示在 ScreenShot 中。

截图:1

截图:2

更新:

替换你的代码

calendar.setTimeInMillis(calendar.getTimeInMillis());
        long mindate = calendar.getTime().getTime();

        dpd.getDatePicker().setMinDate(mindate);

有了这个

  dpd.getDatePicker().setMinDate(System.currentTimeMillis());

我在我的主题中使用了这种风格:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="colorPrimary">@color/green_sheet</item>
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <item name="android:textAllCaps">false</item>     

</style> 

textColorPrimary 属性 以白色显示所有天数,我使用此 属性 将操作栏文本设置为白色。我错了,因为我必须为此使用 Theme.AppCompat.Light.DarkActionBar,所以我将主题样式更改为:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <item name="colorPrimary">@color/green_sheet</item>

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

</style>

结果如预期。

DatePicker / DatePickerDialog 在 Android Marshmallow / API 23 上没有正确显示是一个主题问题。我建议,不要使用默认主题或编写自定义样式,而是使用“theme”选项实例化 DatePickerDialog

DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth)

API 23 起,Material 主题应用于对话框。使用 android.R.style.Theme_Material_Light_Dialog_Alert 作为主题参数。

对于 Xamarin Android - 使用 Android.Resource.Style.ThemeMaterialLightDialogAlert.