Android DatePickerDialog Api 23(Android 6)

时间:2016-06-25 04:08:54

标签: android android-6.0-marshmallow android-5.1.1-lollipop

我正在触发一个DatePickerDialog,它工作正常,直到api 22(Android 5.1),我正在设置混合和最大日期(min =当前日期,max =从当前日期开始的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);

    }
} 

Android 5.1

Android 6

答案1:结果ANSWER 1: Results

3 个答案:

答案 0 :(得分:1)

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

<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>

,结果与预期一致。

答案 1 :(得分:1)

DatePicker / DatePickerDialog在Android Marshmallow / API 23上无法正常显示是一个主题问题。我建议,使用&#39; 主题&#39;来代替使用默认主题或编写自定义样式,实例化 DatePickerDialog 。选项:

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

API 23以后,材质主题应该用于对话框。使用android.R.style.Theme_Material_Light_Dialog_Alert作为主题参数。

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

答案 2 :(得分:0)

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,并根据您的要求选择MaxDate 1个月,它会在ScreenShot中显示。

ScreenShot:1

enter image description here

ScreenShot:2

enter image description here

更新:

替换此代码

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

        dpd.getDatePicker().setMinDate(mindate);

用这个

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