gpt4 book ai didi

android - 日期选择器 Android Lollipop 中未调用 OnDateChanged

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:41 25 4
gpt4 key购买 nike

我有自定义日期选择器和时间选择器。它在 api 级别 21 之前工作正常,但在它不工作之后。我调试了代码,发现更改日期时不会调用 ondatechangedontimechanged

那么如何解决这个问题。

DatePicker 的自定义类是 -

public class DatePickerDialog extends Dialog implements OnDateChangedListener,OnClickListener{

private static DatePickerDialog dialog;
private String date;
private String title;
private DatePickerCallback callback;
private DatePicker datePicker;
private TextView tvHeading;
private Button btnDone;
private String lastModifiedDate;
private String initialDate;
private boolean isStartDateToday;
private long currentDateInMS = 0;
private Context context;
private int noOfDaysBefore = 0;

public DatePickerDialog(Context context, DatePickerCallback callback, String date, String title, boolean isStartDateToday,int noOfDaysBefore) {
super(context);
this.context = context;
this.callback = callback;
this.date = date;
this.lastModifiedDate = date;
this.initialDate = date;
this.title = title;
this.isStartDateToday = isStartDateToday;
this.noOfDaysBefore = noOfDaysBefore;
}

public interface DatePickerCallback {
public void setDate(String date);
}

public static void showDatePickerDialog(Context context,DatePickerCallback callback,String date,String title,int numOfDaysBefore){
dialog = new DatePickerDialog(context, callback, date,title,false,numOfDaysBefore);
dialog.show();
}

public static void showDatePickerDialogStartDateToday(Context context,DatePickerCallback callback,String date,String title){
dialog = new DatePickerDialog(context, callback, date,title,true,0);
dialog.show();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_datepicker);
getWindow().getAttributes().windowAnimations = R.style.animationdialog;
findAllIds();

setListener();
init(date);
}

private void findAllIds(){
tvHeading = (TextView)findViewById(R.id.tv_heading);
datePicker = (DatePicker)findViewById(R.id.datePicker);
btnDone = (Button)findViewById(R.id.btn_done);
tvHeading.setText(title);
}

private void setListener(){
btnDone.setOnClickListener(this);
}

private long getMilisecondsFromCalendar(int noOfDays){
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy HH:mm:ss");
String previousDate = CommonFunction.getDateBeforeOrAfterNoOfDays(
CommonFunction.getCurrentDate("ddMMyyyy HH:mm:ss"), noOfDaysBefore, "ddMMyyyy HH:mm:ss");
Date date;
Calendar calendar = Calendar.getInstance();
try {
date = sdf.parse(previousDate);
calendar.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}

return calendar.getTimeInMillis();
}

private void init(String date){
String date_str = CommonFunction.formatDate("yyyyMMdd", "dd MM yyyy", date);
String datearr[] = date_str.split(" ");
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(datearr[2]), Integer.parseInt(datearr[1]), Integer.parseInt(datearr[0]));
int iDay=calendar.get(Calendar.DATE);
int iMonth=calendar.get(Calendar.MONTH);
int iYear=calendar.get(Calendar.YEAR);
if(isStartDateToday){
datePicker.setMinDate(System.currentTimeMillis()-1000);
}else{
if(currentDateInMS == 0){

currentDateInMS = getMilisecondsFromCalendar(1);//Calendar.getInstance().getTimeInMillis();
}
datePicker.setMaxDate(currentDateInMS);
}
datePicker.init(iYear, iMonth-1, iDay, this);
datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
}

@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
StringBuilder date = new StringBuilder();
date.append(year);
monthOfYear = monthOfYear +1;
if(monthOfYear < 10){
date.append("0"+monthOfYear+dayOfMonth);
}else{
date.append(monthOfYear).append(dayOfMonth);
}
if(isStartDateToday){

}else{
if(!isDateValid(date.toString())){
try{
init(lastModifiedDate);
}catch(Exception e){
e.printStackTrace();
}
return;
}
}
this.date = date.toString();
}

private boolean isDateValid(String date){
boolean flag = false;
String currentDate = CommonFunction.getCurrentDate("yyyyMMdd");
long days = CommonFunction.getNoOfDaysBetweenDate(currentDate, date, "yyyyMMdd");
// long days = CommonFunction.getNoOfDaysBetweendate(CommonFunction.formatDate("dd-MMM-yyyy", "ddMMyyyy", ), CommonFunction.formatDate("yyyyMMdd", "ddMMyyyy", date));
if(days <= 0){
flag = true;
lastModifiedDate = date;
}
return flag;
}

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_done:
callback.setDate(this.date);
dialog.dismiss();
break;
}
}
}

布局文件是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#88000000"
>

<TextView
android:id="@+id/tv_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/topcornerblue"
android:padding="@dimen/datePickerHeadingTextPadding"
android:text="@string/selectDate"
android:textColor="@color/white"
android:textSize="@dimen/datePickerHeadingTextSize" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/whitebottomcorner"
android:paddingBottom="@dimen/datePickerButtonMargin"
android:layout_below="@+id/tv_heading" >

<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/datePickerMarginTop" />


<Button
android:id="@+id/btn_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/datePickerMarginTop"
android:background="#14768D"
android:padding="@dimen/datePickerHeadingTextPadding"
android:text="@string/done"
android:textColor="@color/white"
android:minWidth="@dimen/datePickerButtonMinWidth"
android:minHeight="@dimen/datePickerButtonMinHeight"
android:textSize="@dimen/datePickerButtonTextSize"
android:visibility="visible" />
</RelativeLayout>

</RelativeLayout>

最佳答案

这是 Android 5.0 及更高版本中的一个错误,当日期选择器处于其新的 Material 样式日历模式时会发生。您可以通过在日期选择器上设置 android:datePickerMode="spinner" 强制日期选择器使用 5.0 之前的微调器模式来解决此错误。

或者您可以使用 DatePickerDialog反而。对话框监听器的 onDateChanged 方法会被调用,即使对话框处于新 Material 日历模式也是如此。

关于android - 日期选择器 Android Lollipop 中未调用 OnDateChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31609777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com