安卓日历对话框Api 23 (安卓6)

4

我触发了一个DatePickerDialog,它在api 22(Android 5.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:结果答案1:结果

这些内容涉及到IT技术,展示了两个安卓版本的图片,并提供了一个答案的图标。

错误在这几行代码中:long mindate = System.currentTimeMillis(); dpd.getDatePicker().setMinDate(mindate); - Jose Ricardo Citerio Alcala
好的,我会把这些行合并成一行。 - Jose Ricardo Citerio Alcala
你懂了吗? - Harshad Pansuriya
伙计,我在这行代码还是得到同样的错误提示:dpd.getDatePicker().setMinDate(System.currentTimeMillis()); - Jose Ricardo Citerio Alcala
请查看此帖子https://dev59.com/yGYr5IYBdhLWcg3wdp4C - Jose Ricardo Citerio Alcala
显示剩余7条评论
3个回答

3

在Android Marshmallow / API 23上,DatePicker / DatePickerDialog无法正常显示是一个主题问题。我建议,不要使用默认主题或编写自定义样式,而是使用“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


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>

并且结果如预期。


0
在你的DatePicker中,你已经将MinDate设置为currentTimeMillis(),并且在你的MaxDate中,你已经将Calendar加上了1 MONTH
只需简单地使用此代码即可。
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();


            }

        });

    }

在我的日期选择器中,我有一个日期是June 25,如果你有一个MaxDate限制为1个月,那么它会展示在屏幕截图上。 屏幕截图: 1

enter image description here

屏幕截图:2

enter image description here

更新:

请替换您的这段代码

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

        dpd.getDatePicker().setMinDate(mindate);

使用这个

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

我在 Android 模拟器上测试了我的代码(问题中的那个),它运行良好,日历看起来像你的,但在真实设备上(我上面附加的图片属于 Motorola g3 手机)可能会有所不同。 - Jose Ricardo Citerio Alcala
这是一个主题问题,星期几显示为白色而不是黑色(允许的),以及灰色(不允许的)。 - Jose Ricardo Citerio Alcala
想象一下,在您的示例中,所有日期数字都是白色的,但当前日期不是。 - Jose Ricardo Citerio Alcala
@user4216713 因为它被选为“日期”,所以它的颜色是“白色”。 - Harshad Pansuriya
这是一个很好的答案,但我的代码没问题,就像我说的,这是与应用程序主题相关的问题(Api 23)。 - Jose Ricardo Citerio Alcala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接