Android Date Picker材质样式 [英] Android Date Picker Material Style

本文介绍了Android Date Picker材质样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have implemented the date picker on my fragment, this is the code:

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


            });

和我的日期选择器的样式是这样的旧样式:

and the style of my date picker is a old style as this :

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

I would like to use a DatePicker with material style, i have tried to use this library compile 'com.wdullaer:materialdatetimepicker:2.3.0', but the result is the same. Any help how i can change this DatePicker with Material Date Picker? Thanks

推荐答案

尝试一下: 阿森纳的DatePicker材料

如有疑问,请参阅我的github存储库

If any doubt see my github Repository

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

您的结果将如下所示:-

Your result will look like this:-

希望这会帮助您....

Hope this will help you thanks....

这篇关于Android Date Picker材质样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆