OnDateChanged在日期选择器Android棒棒糖中未调用
我有自定义日期选择器和时间选择器。它在api级别21之前工作正常,但在api级别21之后不工作。我调试了代码,知道更改日期时不会调用ondatechanged和ontimechanged 那么如何解决这个问题呢 日期选择器的自定义类为-OnDateChanged在日期选择器Android棒棒糖中未调用,android,Android,我有自定义日期选择器和时间选择器。它在api级别21之前工作正常,但在api级别21之后不工作。我调试了代码,知道更改日期时不会调用ondatechanged和ontimechanged 那么如何解决这个问题呢 日期选择器的自定义类为- public class DatePickerDialog extends Dialog implements OnDateChangedListener,OnClickListener{ private static DatePickerDialog
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;
}
}
}
公共类DatePickerDialog扩展对话框实现OnDateChangedListener、OnClickListener{
专用静态日期选择器对话框;
私有字符串日期;
私有字符串标题;
私有DatePickerCallback回调;
私人日期选择器日期选择器;
私有文本视图标题;
私人按钮btnDone;
私有字符串lastModifiedDate;
私有字符串初始日期;
今天开始的私人布尔运算;
私有长currentDateInMS=0;
私人语境;
private int noOfDaysBefore=0;
公共DatePickerDialog(上下文上下文、DatePickerCallback回调、字符串日期、字符串标题、布尔值isStartDateToday、int noOfDaysBefore){
超级(上下文);
this.context=上下文;
this.callback=回调;
this.date=日期;
this.lastModifiedDate=日期;
this.initialDate=日期;
this.title=标题;
this.isStartDateToday=isStartDateToday;
this.noOfDaysBefore=noOfDaysBefore;
}
公共接口DatePickerCallback{
公共作废设置日期(字符串日期);
}
公共静态void showDatePickerDialog(上下文上下文、DatePickerCallback回调、字符串日期、字符串标题、int numOfDaysBefore){
dialog=newdatepickerdialog(上下文、回调、日期、标题、false、numOfDaysBefore);
dialog.show();
}
公共静态void showDatePickerDialogStartDateToday(上下文上下文、DatePickerCallback回调、字符串日期、字符串标题){
dialog=newdatepickerdialog(上下文、回调、日期、标题、true、0);
dialog.show();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(R.layout.layout\u日期选择器);
getWindow().getAttributes().windowAnimations=R.style.animationdialog;
findAllIds();
setListener();
初始(日期);
}
私有void findAllIds(){
tvHeading=(TextView)findViewById(R.id.tv_标题);
datePicker=(datePicker)findViewById(R.id.datePicker);
btnDone=(按钮)findViewById(R.id.btn_done);
tvHeading.setText(标题);
}
私有void setListener(){
setOnClickListener(这个);
}
日历上的私有长GetMilsSeconds(整数中午){
SimpleDataFormat sdf=新的SimpleDataFormat(“ddMMyyyy HH:mm:ss”);
字符串previousDate=CommonFunction.GetDateBeforeAfterNoofDays(
getCurrentDate(“ddmmyyyyy HH:mm:ss”),noOfDaysBefore,“ddMMyyyy HH:mm:ss”);
日期;
日历=Calendar.getInstance();
试一试{
date=sdf.parse(上一个日期);
日历。设置时间(日期);
}捕获(解析异常){
e、 printStackTrace();
}
return calendar.getTimeInMillis();
}
私有void init(字符串日期){
字符串日期\u str=CommonFunction.formatDate(“yyyyymmdd”,“dd-MM-yyyy”,date);
字符串datearr[]=date_str.split(“”);
日历=Calendar.getInstance();
calendar.set(Integer.parseInt(datearr[2])、Integer.parseInt(datearr[1])、Integer.parseInt(datearr[0]);
intIDay=calendar.get(calendar.DATE);
int iMonth=calendar.get(calendar.MONTH);
int iYear=calendar.get(calendar.YEAR);
如果(今天开始){
datePicker.setMinDate(System.currentTimeMillis()-1000);
}否则{
如果(currentDateInMS==0){
currentDateInMS=GetMilisSecondsFromCalendar(1);//Calendar.getInstance().getTimeInMillis();
}
datePicker.setMaxDate(currentDateInMS);
}
datePicker.init(iYear,iMonth-1,iDay,this);
datePicker.SetDegenantFocusability(datePicker.FOCUS\u块\u子体);
}
@凌驾
公共无效onDateChanged(日期选择器视图、整数年、整数月),
整数(每月的第几天){
StringBuilder日期=新建StringBuilder();
日期。追加(年);
monthOfYear=monthOfYear+1;
如果(每年的月数<10){
日期。附加(“0”+月份+月日);
}否则{
日期。附加日期(月日)。附加日期(月日);
}
如果(今天开始){
}否则{
如果(!isDateValid(date.toString())){
试一试{
init(lastModifiedDate);
}捕获(例外e){
e、 printStackTrace();
}
返回;
}
}
this.date=date.toString();
}
私有布尔值isDateValid(字符串日期){
布尔标志=假;
字符串currentDate=CommonFunction.getCurrentDate(“yyyyMMdd”);
long days=CommonFunction.GetNoofDaysBetween日期(currentDate,日期,“yyyyymmdd”);
//long days=CommonFunction.getnoofdaysbetween-date(CommonFunction.formatDate(“dd-MMM-yyyy”,“ddMMyyyy”,“ddMMyyyy”),CommonFunction.formatDate(“yyyyymmdd”,“ddmmyyyyy”,date));
if(days这是Android 5.0及更高版本中的一个bug,当datepicker处于其新材质样式日历模式时发生。
您可以通过在日期选择器上设置android:datePickerMode=“spinner”
强制日期选择器使用5.0之前的微调器模式来解决此错误
或者您可以改为使用。对话框侦听器的onDateChanged
方法确实会被调用,即使对话框处于新材质日历模式。阅读代码时,我从未看到您在任何地方分配OnDateChangedListener。您实现了这些方法,但我
<?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>