Android的DataPickerDialog onDateChanged不点火 [英] Android DataPickerDialog onDateChanged is not firing
问题描述
我有一个问题,下面从Android的教程步骤DatePickerDialog。
I've a problem with a DatePickerDialog following steps from Android Tutorial.
链接一个TextView推出DatePickerDialog时被聚焦,你可以看到如下:
Linking a TextView to launch a DatePickerDialog when gets focused as you can see below:
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类:
Works fine, the DatePickerDialog is launched. I instance a new DatePickerFragment class that is created in this way:
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方法来更改对话框的标题,但它永远不会被调用。我试图创建一个onDateChangedListener但DatePickerDialog没有之一。
And here is the problem. I want to change Dialog's title using onDateChanged method, but it's never called. I tried to create a onDateChangedListener but DatePickerDialog doesn't have one.
我怎样才能做到这一点?
How can i do this?
在此先感谢。
推荐答案
简单地扩展DatePickerDialog类并覆盖onDateChanged如下图所示。我加入了一个新的构造函数,它接受一个日历对象并设置了年,月,日本身,为您节省了不必要的打字。 (我不明白为什么这不是一个选项了。)反正这里是如何使用类和较短的构造:
Simply extend the DatePickerDialog class and override onDateChanged like below. I included a new constructor that accepts a Calendar object and sets the year, month and day itself, saving you the unnecessary typing. (I don't understand why this isn't an option already.) Anyway here is how to use the class and the shorter constructor:
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");
}
}
这篇关于Android的DataPickerDialog onDateChanged不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!