我的 DialogFragment 类在我按下 Activity 按钮时被调用。我希望通过此 DialogFragment 设置的日期显示在按钮文本上。 FindViewById 拒绝被类识别。
我找到了一个 similar question但我无法将其与我的情况联系起来。
代码:
调用对话 fragment 的按钮:
public void datepicker(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); }
对话 fragment 代码:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceSateate) { 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); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen } } }
最佳答案
在您的 DialogFragment 中负责在用户设置日期时收到通知的方法中执行此操作
Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);
关于android - 从 DialogFragment (DatePicker) 检索和设置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14024921/