public class DatePickerDialogFragment extends DialogFragment {
private Context context;
public DatePickerDialogFragment(Context context) {
this.context = context;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
return new DatePickerDialog(context, (OnDateSetListener) context,
c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
}
public class mActivity extends SherlockFragmentActivity implements
OnDateSetListener {
dateField.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
DialogFragment newFragment = new DatePickerDialogFragment(mActivity.this);
newFragment.show(fm, "date_frag");
}
});
}
无法将 Context( of Activity which impletements OnDateChangedListener
) 强制转换为OnDateSetListener。