按照 Android 教程中的步骤,我遇到了 DatePickerDialog 问题。
链接 TextView 以在获得焦点时启动 DatePickerDialog,如下所示:
EditText fNac = (EditText)findViewById(R.id.regFecha);
fNac.setonfocusChangeListener(new View.onfocusChangeListener(){
@Override
public void onfocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
// Desde aquí lanzamos el datepicker
if (hasFocus){
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager() , "datePicker");
}
}
工作正常,DatePickerDialog 已启动。我实例化了一个以这种方式创建的新 DatePickerFragment 类:
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private DatePickerDialog mDatePickerDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
// Usamos la fecha actual como primera fecha a mostrar
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Devolvemos la instancia de un Dialog con la fecha actual
mDatePickerDialog = new DatePickerDialog(getActivity(),this,year,month,day);
return mDatePickerDialog;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// TODO Auto-generated method stub
Log.v("LOG_CAT","OK");
}
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
Log.v("LOG_CAT", "Ok2");
}
}
这就是问题所在。我想使用 onDateChanged 方法更改 Dialog 的标题,但从未调用过。我试图创建一个 onDateChangedListener 但 DatePickerDialog 没有。
我怎样才能做到这一点?
提前致谢。
最佳答案
只需扩展 DatePickerDialog 类并覆盖 onDateChanged ,如下所示。我包含了一个新的构造函数,它接受一个 Calendar 对象并设置年、月和日本身,从而为您节省了不必要的输入。 (我不明白为什么这不是一个选项。)无论如何,这里是如何使用类和较短的构造函数:
public class DatePickerFragment extends DialogFragment implements OnDateSetListener {
public class MyDatePickerDialog extends DatePickerDialog {
private Calendar calendar;
private final String format = "EEEE, MMMM dd, yyyy";
// Regular constructor
public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
calendar = Calendar.getInstance();
}
// Short constructor
public MyDatePickerDialog(Context context, OnDateSetListener callBack, Calendar calendar) {
super(context, callBack, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
this.calendar = calendar;
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
calendar.set(year, month, day);
setTitle(DateFormat.format(format, calendar));
}
}
private MyDatePickerDialog mDatePickerDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mDatePickerDialog = new MyDatePickerDialog(this, this, Calendar.getInstance());
return mDatePickerDialog;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Log.v("DatePickerFragment", "onDateSet");
}
}
关于java - Android DataPickerDialog onDateChanged 未触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11886514/