我触发了一个DatePickerDialog,它正在工作,显示很好直到api 22 (Android5.1),我在上面设置了混合和最大日期(min =当前日期,max =从当前日期开始的1个月),但是它只是显示Api 23中的当前日期,我附加了代码和图像。
///////////////////////////////datepickerdialog///////////////////////////////
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
/*
Create a DatePickerDialog using Theme.
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
int year, int monthOfYear, int dayOfMonth)
*/
// DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
DatePickerDialog dpd = new DatePickerDialog(getActivity(),this,year,month,day);
if (Build.VERSION.SDK_INT < 22){
dpd.getDatePicker().setCalendarViewShown(true);
dpd.getDatePicker().setSpinnersShown(false);
dpd.getDatePicker().getCalendarView().setShowWeekNumber(false);
}
calendar.setTimeInMillis(calendar.getTimeInMillis());
long mindate = calendar.getTime().getTime();
calendar.add(Calendar.MONTH, 1);
long maxdate = calendar.getTime().getTime();
dpd.getDatePicker().setMinDate(mindate);
dpd.getDatePicker().setMaxDate(maxdate);
dpd.getDatePicker().setMinDate(mindate);
if(Build.VERSION.SDK_INT < 23){
dpd.setTitle("");
}
// Return the DatePickerDialog
return dpd;
}
@SuppressLint("SimpleDateFormat")
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
month++;
evento_fecha = year+"-"+month+"-"+day;
month--;
datetime.set(Calendar.YEAR, year);
datetime.set(Calendar.MONTH, month);
datetime.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat mSDF = new SimpleDateFormat("dd/MM/yyyy");
fecha = mSDF.format(datetime.getTime());
//fecha_seleccionada.setText(dia+"/"+mes+"/"+year);
fecha_seleccionada.setText(fecha);
}
}


回答1:结果

发布于 2016-06-26 05:03:25
我在我的主题中使用了这种风格:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">@color/green_sheet</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textAllCaps">false</item>
</style> textColorPrimary属性以白色显示所有天数,我使用此属性将操作栏文本设置为白色。我错了,因为我不得不为此目的使用Theme.AppCompat.Light.DarkActionBar,所以我将主题风格改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">@color/green_sheet</item>
<item name="android:textAllCaps">false</item>
</style>结果和预期的一样。
发布于 2016-07-25 07:07:19
DatePicker / DatePickerDialog没有在Android / API 23上正确出现是一个主题问题。我建议,与其使用默认主题或编写自定义样式,不如使用“DatePickerDialog主题”选项实例化该:
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth)API 23以后,材料主题应用于对话框。主题参数使用android.R.style.Theme_Material_Light_Dialog_Alert。
对于,Xamarin,,Android -使用Android.Resource.Style.ThemeMaterialLightDialogAlert。
发布于 2016-06-25 04:27:31
在DatePicker中,将MinDate设置为currentTimeMillis(),在MaxDate中将1 MONTH添加到Calendar。
只需简单地使用这段代码。
public class MainActivity extends Activity {
int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
final TextView textView = (TextView)findViewById(R.id.textview_totalStone);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
c.set(year, month, day);
String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
textView.setText(date);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
Calendar d = Calendar.getInstance();
d.add(Calendar.MONTH,1);
dpd.getDatePicker().setMaxDate(d.getTimeInMillis());
dpd.show();
}
});
}在我的DatePicker中,我有日期、June 25和它选择,根据您的要求,您有MaxDate,1个月,它显示在ScreenShot中。
ScreenShot : 1

ScreenShot : 2

更新:
替换此代码
calendar.setTimeInMillis(calendar.getTimeInMillis());
long mindate = calendar.getTime().getTime();
dpd.getDatePicker().setMinDate(mindate);有了这个
dpd.getDatePicker().setMinDate(System.currentTimeMillis());https://stackoverflow.com/questions/38024857
复制相似问题