Android Date Picker材质样式

APP*_*GIS 5 android datepicker material-design material-components material-components-android

我已经在片段上实现了日期选择器,这是代码:

edittext_from.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Calendar c = Calendar.getInstance();
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);
                    android.app.DatePickerDialog datePickerDialog = new android.app.DatePickerDialog(getContext(),
                            new android.app.DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {

                                    edittext_from.setText(String.format("%04d-%02d-%02d", year, (monthOfYear + 1), dayOfMonth));

                                }
                            }, mYear, mMonth, mDay);
                    datePickerDialog.show();
                }


            });
Run Code Online (Sandbox Code Playgroud)

我的日期选择器的样式是旧样式,如下所示:日期选择器

我想使用具有材料样式的DatePicker,我尝试使用此库compile 'com.wdullaer:materialdatetimepicker:2.3.0',但结果是相同的。任何帮助我如何使用Material Date Picker更改此DatePicker?谢谢

Gab*_*tti 8

借助AndroidMaterial Components,您可以使用新的MaterialDatePicker

目前,它正在积极开发中,并且需要1.1.0版的材料组件才能用于android库。

implementation 'com.google.android.material:material:1.1.0-beta01'
Run Code Online (Sandbox Code Playgroud)

只需使用:

       MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
       builder.setTitleText(R.string.your_text);
       MaterialDatePicker<Long> picker = builder.build();
       picker.show(getSupportFragmentManager(), picker.toString());
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 这应该被认为是最佳答案 (2认同)
  • 如何选择日期 (2认同)

Rah*_*aha 3

试试这个: Arsenal 的 Material DatePicker

如果有任何疑问请参阅我的 github 存储库

GitHUb:- https://github.com/rahulkushwaha482/DatePickerDialogFragment

首先添加这个依赖

implementation('com.shagi:material-datepicker:1.3') {
        exclude group: 'com.android.support'
    }
Run Code Online (Sandbox Code Playgroud)

添加互联网权限

This is required by this project.
Run Code Online (Sandbox Code Playgroud)

1)在 kotlin 中,使用以下代码:-

 val dialog = DatePickerFragmentDialog.newInstance({ view, year, monthOfYear, dayOfMonth ->
                Toast.makeText(applicationContext,
                        "year $year month $monthOfYear day $dayOfMonth",
                        Toast.LENGTH_SHORT).show()
            }, 2017, 11, 4)

            dialog.show(supportFragmentManager, "tag")

            /* Possible params
                dialog.setMaxDate(System.currentTimeMillis())
                dialog.setMinDate(System.currentTimeMillis())
                dialog.setYearRange(2000, 2010)
                dialog.setCancelColor(Color.MAGENTA)
                dialog.setOkColor(Color.MAGENTA)
                dialog.setAccentColor(Color.MAGENTA)
                dialog.setCancelText("Out")
                dialog.setOkText("Fine")
            */
Run Code Online (Sandbox Code Playgroud)

2)在Java中,使用以下代码:-

DatePickerFragmentDialog datePickerFragmentDialog=DatePickerFragmentDialog.newInstance(new DatePickerFragmentDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerFragmentDialog view, int year, int monthOfYear, int dayOfMonth) {
                txtTodayDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);


            }
        },mYear, mMonth, mDay);
        datePickerFragmentDialog.show(getSupportFragmentManager(),null);
        datePickerFragmentDialog.setMaxDate(System.currentTimeMillis());
        datePickerFragmentDialog.setYearRange(1900,mYear);
        datePickerFragmentDialog.setCancelColor(getResources().getColor(R.color.colorPrimaryDark));
        datePickerFragmentDialog.setOkColor(getResources().getColor(R.color.colorPrimary));
        datePickerFragmentDialog.setAccentColor(getResources().getColor(R.color.colorAccent));
        datePickerFragmentDialog.setOkText(getResources().getString(R.string.ok_dob));
        datePickerFragmentDialog.setCancelText(getResources().getString(R.string.cancel_dob));
Run Code Online (Sandbox Code Playgroud)

你的结果将如下所示:-

在此输入图像描述

希望这对你有帮助谢谢......