当我按下活动上的按钮时,会调用我的 DialogFragment 类。我希望通过此 DialogFragment 设置的日期显示在按钮文本上。FindViewById 拒绝被类识别。
我发现了一个类似的问题,但我无法将其与我的案例联系起来。
代码:
调用对话片段的按钮:
public void datepicker(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); }
对话片段代码:
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 } } }