我使用谷歌的例子使用dialogfragment插入我的应用程序内的日期选择器
http://developer.android.com/guide/topics/ui/controls/pickers.html
但我不知道怎么去设定日期后(不是Java专家)。 对话和日期选择器运行正常,我可以日志日期correctely设置,但是,我该怎么做才能得到父母的活动执行的回调?
这是我的对话片段
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
**Log.w("DatePicker","Date = " + year);**
}
}
......我叫对话框从我的活动...
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
什么是叫我的父母,而不是活动的一个Log.w方法,正确的方法是什么? 我想这是一些与传递一个回调参数或者相关的东西,但大多数我因特网上发现引用的是关于以前的版本不dialogfragments
编辑:不知道这是否是重要的,但父活动被声明为:
public class EditSessionActivity extends FragmentActivity {
解决方案:由于Lecho用户这是做它的方式
DatePickerFragmennt.class
public class DatePickerFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
}
}
...和父活动EditSessionActivity.class ...
public class EditSessionActivity extends FragmentActivity implements OnDateSetListener {
...
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((EditText) findViewById(R.id.tf_date)).setText("Date = " + year);
}
Answer 1:
构造函数FO DatePickerDialog
需要DatePickerDialog.OnDateSetListener
作为第二个参数,那么也许你应该实现你的父母的活动,接口EditSessionActivity
(不DatePickerFragment
)和改变这一行:
return new DatePickerDialog(getActivity(), this, year, month, day);
这个:
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
然后你应该活动如下:
public class EditSessionActivity extends FragmentActivity implements
DatePickerDialog.OnDateSetListener{
public void onDateSet(DatePicker view, int year, int month, int day) {
//use date in your activity
}
...
}
Answer 2:
我简单的覆盖onDateSet日期选取器创建在我的课,显示日期选择器。 请参见下面的代码:
private OnClickListener onDateClicked() {
return new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
salePaymentCustomView.setBtDateText("" + day + "/" + month+1 + "/" + year);
}
};
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
};
}
Answer 3:
这个问题也可以在不移动解决onDateSet
方法父活动。 你只需要告诉DatePickerFragment
哪里搜索的观点:
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((TextView) getActivity().findViewById(R.id.tf_date)).setText("Date = " + year);
}
}
我也改变了转换的目标从EditText
到TextView
。 这样,您就可以重用为不同类型的意见,不仅对实现EditText
。
Answer 4:
我有一个修正上述答案。 而不是返回DatePickerDialog这样
返回新DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日);
你应该在一个更通用的方式返回
返回新DatePickerDialog(getActivity(),(OnDateSetListener)getActivity(),年,月,日);
只要确保你的活动实现OnDateSetListener接口和重写功能onDateSet
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
Answer 5:
public void onDate(View view) {
DialogFragment fragment = new SelectDateFragment();
fragment.show(getFragmentManager(), "Date Picker");
}
// @SuppressLint("NewApi")
class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
public Dialog onCreateDialog(Bundle savedInstanceState) {
System.out.println("entrering on create dialog");;
return new DatePickerDialog(getActivity(), this, mYear, mMonth,
mDay);//it will return dialog setting date with mYera,MMonth and MDay
}
@Override
public void onDateSet(android.widget.DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
System.out.println("year=" + year + "day=" + dayOfMonth + "month="
+ monthOfYear);
mYear=year;
mMonth=monthOfYear;
mDay=dayOfMonth;
onPopulateSet(year, monthOfYear + 1, dayOfMonth);
}
// set the selected date in the edit text
private void onPopulateSet(int year, int i, int dayOfMonth) {
EditText et_setDate;
et_setDate = (EditText) findViewById(R.id.register_et_dob);//register_et_dob:-id name of the edit text
et_setDate.setText(dayOfMonth + "/" + i + "/" + year);
System.out.println("enetring on populate Set");
}
Answer 6:
莱谢克的答复工作,但如果对话框是从另一个片段开始。 在这种情况下,你需要使用setTargetFragment()
在创建对话框和代码getTargetFragment()
在对话框的代码。
在创建该对话框的代码:
DialogFragment dialogFragment = new YourDialogFragment();
dialogFragment.setTargetFragment(YourParentFragment.this, 0);
dialogFragment.show(getFragmentManager(), "mydialog");
在DialogFragment代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
YourParentFragment parentFragment = (YourParentFragment) getTargetFragment();
// Manipulate the parent fragment
// ...
}
};
// Get the initial date
// ...
return new DatePickerDialog(getActivity(), listener, year, month, day);
}
Answer 7:
如果你有一个结果返回到一个片段,但不是一个活动,这里是一个完整的解决方案:
public class DatePickerDialogFragment extends DialogFragment {
private static final String ARGUMENT_YEAR = "ARGUMENT_YEAR";
private static final String ARGUMENT_MONTH = "ARGUMENT_MONTH";
private static final String ARGUMENT_DAY = "ARGUMENT_DAY";
private DatePickerDialog.OnDateSetListener listener;
private int year;
private int month;
private int dayOfMonth;
public static DatePickerDialogFragment newInstance(final int year, final int month, final int dayOfMonth) {
final DatePickerDialogFragment df = new DatePickerDialogFragment();
final Bundle args = new Bundle();
args.putInt(ARGUMENT_YEAR, year);
args.putInt(ARGUMENT_MONTH, month);
args.putInt(ARGUMENT_DAY, dayOfMonth);
df.setArguments(args);
return df;
}
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
retrieveArguments();
}
private void retrieveArguments() {
final Bundle args = getArguments();
if (args != null) {
year = args.getInt(ARGUMENT_YEAR);
month = args.getInt(ARGUMENT_MONTH);
dayOfMonth = args.getInt(ARGUMENT_DAY);
}
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
return new DatePickerDialog(getContext(), this.listener, this.year, this.month, this.dayOfMonth);
}
public void setListener(final DatePickerDialog.OnDateSetListener listener) {
this.listener = listener;
}
}
然后,只需用它在一个片段或一个活动:
final Calendar c = Calendar.getInstance();
DatePickerDialogFragment datePicker = DatePickerDialogFragment.newInstance(
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
datePicker.setListener(this);
datePicker.show(getChildFragmentManager(), null);
Answer 8:
我有一个类似的问题这一点,但我的日期选择器正在从另一个片段中的对话片段推出。 这使得它很难玩的回调,因为他们要返回到主活动,我想将数据返回到先前的对话片段。
最初,我通过了新的日期值的主要活动(使用OnDateSetListener),并打算使用推出日期选取器对话框,让他们,但也有在该对话框不会触发生命周期事件时的日期选择器关闭。
我来是onDismiss日期选取器,实例化一个新的对话片段,在其上调用一组结果的方法,然后启动它的结果。 当然,为此,你需要确保当它启动的日期选择器上一片段驳回。
这就是所谓的日期选取器的dialogFragment
public class NewTrialDialogFragment extends DialogFragment {
public final String LOG_TAG = "NewTrialDialog Fragment";
Button startDateButton;
Integer newYear, newMonth, newDay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_new_trial, container, false);
getDialog().setTitle("Dialog New Trial");
startDateButton = (Button) rootView.findViewById(R.id.button_start_date);
startDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(v);
}
});
//If you exit the datepicker without choosing anything, it returns zeros
if (newYear != null && newMonth != null && newDay != null && newYear != 0) {
startDateButton.setText(newYear + " " + newMonth + " " + newDay);
}else{
startDateButton.setText("Select Start Date");
}
return rootView;
}
public void showDatePickerDialog(View v) {
TrialDatePickerDialog newDialog = new TrialDatePickerDialog();
newDialog.show(getActivity().getSupportFragmentManager(), "datePicker");
this.dismiss();
}
public void setDates(int year, int month, int day){
newYear = year;
newMonth = month;
newDay = day;
}}
和日期选择器
public class TrialDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
private final String LOG_TAG = "TrialDatePickerDialog";
int newYear, newMonth, newDay;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
// Create a new instance of TrialDatePickerDialog and return it
return new DatePickerDialog(getActivity(), this , year, month, day);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
FragmentManager fm = getActivity().getSupportFragmentManager();
NewTrialDialogFragment newTrialDialogFragment = new NewTrialDialogFragment();
newTrialDialogFragment.setDates(newYear, newMonth, newDay);
newTrialDialogFragment.show(fm, "new_trial_dialog");
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
newYear = year;
newMonth = monthOfYear;
newDay = dayOfMonth;
}
}
文章来源: Get date from datepicker using dialogfragment