从DialogFragment(DatePicker)检索和设置数据

krt*_*ush 3 android android-dialogfragment

当我按下活动上的按钮时,将调用我的DialogFragment类.我希望通过此DialogFragment设置的日期显示在按钮文本上.FindViewById拒绝被类识别.

我发现了一个类似的问题,但我无法将其与我的案例联系起来.

码:

  1. 调用Dialogue片段的按钮:

    public void datepicker(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker"); 
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 对话片段代码:

    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
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

A--*_*--C 10

在DialogFragment中负责在用户设置日期时收到通知的方法中,执行此操作

Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);
Run Code Online (Sandbox Code Playgroud)