6

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

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();
                }


            });

我的日期选择器的风格是这样的旧风格:日期选择器

我想使用具有材质风格的 DatePicker,我尝试使用这个库 compile 'com.wdullaer:materialdatetimepicker:2.3.0',但结果是一样的。有什么帮助我可以用 Material Date Picker 更改这个 DatePicker 吗?谢谢

4

4 回答 4

19

借助适用于 Android 的 Material Components,您可以使用新的MaterialDatePicker.

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

implementation 'com.google.android.material:material:1.1.0'

只需使用:

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

在此处输入图像描述

于 2019-08-20T16:35:53.903 回答
3

试试这个: 阿森纳的材料日期选择器

如果有任何疑问,请查看我的 github 存储库

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

首先添加这个依赖

implementation('com.shagi:material-datepicker:1.3') {
        exclude group: 'com.android.support'
    }

添加 Internet 权限

This is required by this project.

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")
            */

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

您的结果将如下所示:-

在此处输入图像描述

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

于 2019-03-30T12:11:36.037 回答
1

试试这个库: https ://github.com/code-troopers/android-betterpickers

您使用日期选择器,如:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DatePickerBuilder dpb = new DatePickerBuilder()
                .setFragmentManager(getSupportFragmentManager())
                .setStyleResId(R.style.BetterPickersDialogFragment)
                .setYearOptional(true);
        dpb.show();
    }
});
于 2017-02-14T11:49:29.087 回答
0

试试这个,它会工作

repositories{
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
    compile 'com.github.jaydeep17:datetimepicker:0.0.4'
}

https://github.com/jaydp17/datetimepicker

https://github.com/flavienlaurent/datetimepicker

http://www.viralandroid.com/2015/11/android-date-picker-example.html

在此处输入图像描述

于 2017-02-14T11:54:17.887 回答