Android Date Picker Material Style

我的梦境 提交于 2019-12-01 18:14:37

问题


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 :

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


回答1:


Try this : Material DatePicker By Arsenal

If any doubt see my github Repository

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

First add this dependencies

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

Add permission for Internet

This is required by this project.

1)In kotlin ,use this code:-

 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)In Java , use this code:-

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




回答2:


With the Material Components for Android you can use the new MaterialDatePicker.

Currently it is under active development and requires version 1.1.0 of material components for android library.

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

Just use:

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




回答3:


Try this library: https://github.com/code-troopers/android-betterpickers

You use the date picker like:

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



回答4:


Try this one it will work

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



来源:https://stackoverflow.com/questions/42225209/android-date-picker-material-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!