我在我的片段上实现了日期选择器,这是代码:
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,我试图使用这个库编译'com.wdullaer:materialdatetimepicker:2.3.0'
,但结果是一样的。
有关如何使用Material Date Picker更改此DatePicker的任何帮助?
感谢
答案 0 :(得分:9)
有了Material Components for Android,您可以使用新的MaterialDatePicker
。
当前它正在积极开发中,并且需要版本 1.1.0 的Android库的材料组件。
implementation 'com.google.android.material:material:1.1.0-beta01'
只需使用:
MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText(R.string.your_text);
MaterialDatePicker<Long> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
答案 1 :(得分: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();
}
});
答案 2 :(得分:1)
尝试一下: Material DatePicker By Arsenal
如有疑问,请参阅我的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));
您的结果将如下所示:-
希望这会帮助您。...
答案 3 :(得分: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