Rus*_*art 6 android android-datepicker android-alertdialog android-styles
我正在构建一个使用DatePickerDialog的应用程序,允许用户选择他们的生日.这是现在加载对话框的代码:
private void selectBirthdate() {
int year, month, day;
if (mBirthDate == null) {
year = DEF_YEAR;
month = DEF_MON;
day = DEF_DAY;
}
else {
year = mBirthDate.get(Calendar.YEAR);
month = mBirthDate.get(Calendar.MONTH);
day = mBirthDate.get(Calendar.DAY_OF_MONTH);
}
new DatePickerDialog(
getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mBirthDate = new GregorianCalendar();
mBirthDate.set(Calendar.YEAR, year);
mBirthDate.set(Calendar.MONTH, month);
mBirthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (mTxtBirthDate != null) {
mTxtBirthDate.setText(mBirthDateFormat.format(mBirthDate.getTime()));
}
}
},
year,
month,
day
).show();
}
Run Code Online (Sandbox Code Playgroud)
但是,他们希望能够使用旧式微调器DatePicker,因为在新的日历视图中,用户并不总是可以更改年份.所以我一直在研究这个主题,根据我读过的内容,应该可以使用主题强制DatePickerDialog进入Spinner模式.所以这就是我所做的.
(抱歉截图.显然,SO的解析器无法处理XML.)
然后,我更新DatePickerDialog构造函数以使用我的新样式:
new DatePickerDialog(
getActivity(),
R.style.MyDialogTheme,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mBirthDate = new GregorianCalendar();
mBirthDate.set(Calendar.YEAR, year);
mBirthDate.set(Calendar.MONTH, month);
mBirthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (mTxtBirthDate != null) {
mTxtBirthDate.setText(mBirthDateFormat.format(mBirthDate.getTime()));
}
}
},
year,
month,
day
).show();
Run Code Online (Sandbox Code Playgroud)
显然有些事情发生了变化; 对话框的主题较暗.但它没有做我想要的.它仍然显示日历视图而不是微调视图.知道我可能错过了什么吗?
Abu*_*aid 11
这对我有用:
1)在style.xml中创建这两个样式
<style name="MySpinnerDatePickerStyle" parent="android:Theme.Material.Dialog">
<item name="android:datePickerStyle">@style/MySpinnerDatePicker</item>
</style>
<style name="MySpinnerDatePicker" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
Run Code Online (Sandbox Code Playgroud)
2)在新的DatePickerDialog中将"MySpinnerDatePickerStyle"设置为您的样式
DatePickerDialog datePickerDialog = new DatePickerDialog(myContext, R.style.MySpinnerDatePickerStyle, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
Run Code Online (Sandbox Code Playgroud)
希望这对你有用..注意:我仍然试图通过改变颜色和文本颜色来弄清楚如何设置微调器的样式
与@Abuzaid 类似,在您的 AppTheme 样式中,添加项目android:datePickerStyle,如下面的 AppTheme 示例所示:
<!-- Example of a Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<!-- !Important! Add this to force the use of the Spinner mode -->
<item name="android:datePickerStyle">@style/myDatePickerStyle</item>
</style>
Run Code Online (Sandbox Code Playgroud)
将此样式添加到您的 styles.xml (在 value-v21 文件夹内,因为此选项仅适用于 api 21+)
<style name="myDatePickerStyle" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
Run Code Online (Sandbox Code Playgroud)
对于 Android 版本 < 21,默认为微调模式。对于 21 岁以上,日历模式是新的默认模式。
在这种情况下,您可以省略指定对话框主题,因为它将由我们拥有的 AppTheme 推断出来。例如:(使用 Java 8 lambda)
final Calendar cal = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateSelectedListener = (view, year, monthOfYear, dayOfMonth) -> {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear+1);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
};
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dp= new DatePickerDialog(context, dateSelectedListener, year, month, day);
Run Code Online (Sandbox Code Playgroud)
参考: https: //developer.android.com/reference/android/app/DatePickerDialog
希望它能有所帮助!:)
| 归档时间: |
|
| 查看次数: |
3833 次 |
| 最近记录: |