[英]Android DatePickerDialog Api 23 (Android 6)
我正在觸發一個DatePickerDialog,它工作正常,直到api 22(Android 5.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);
}
}
我在我的主題中使用這種風格:
<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>
結果如預期。
DateMicker / DatePickerDialog在Android Marshmallow / API 23上無法正常顯示是一個主題問題。 我建議,使用' theme '選項實例化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
。
在您的DatePicker
您已將MinDate
設置為currentTimeMillis()
並在MaxDate
為Calendar
添加1 MONTH
。
只需簡單使用此代碼即可。
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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.