android如何调用扩展的类的成员函数 DialogFragment 中Activity

发布时间: 2017/3/19 17:28:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我已经写了自定义 DatePickerDialog 延伸 DialogFragment 并实现了 DatePickerDialog.OnDateSetListener

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                R.style.MyDialogTheme, this, year, month, day);
    return dpd;
  }

  public void onDateSet(DatePicker view, int year, int month, int day) {

  }

  // own  methods
  String getFormattedDate() {
  }
}

我把这个叫做 fragment 的我的 MainActivity。

public class Booking_activity extends AppCompatActivity implements View.OnClickListener {

  public void onClick(View view) {
    if (view == imgView_from) {
      // Initialize a new date picker dialog fragment
      DialogFragment dFragment = new DatePickerFragment();
      // Show the date picker dialog fragment
      dFragment.show(getFragmentManager(), "Date Picker");

      // error 
      dFragment.getFormattedDate();
    } 
  } 
}

解决方法 1:

错误发生因为你试图访问无效的方法,从 DialogFragment 。您需要创建与 DatePickerFragment:

DatePickerFragment dFragment = new DatePickerFragment();

或铸造它︰

DialogFragment dFragment = new DatePickerFragment();
((DatePickerDialog)dFragment).getFormattedDate();

主要的缺陷是,你试着访问 getFormattedDate() methoddFragment 对象已完成实例化。请记住, show() 是一个异步调用。

所以你需要告知 Booking_activity activitydFragment 已完成实例化。你可以为它使用一个接口。一些类似于以下内容︰

  1. 在你的 DatePickerFragment,添加界面︰

    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    
      private DatePickerListener mListener;
    
      public interface DatePickerListener {
        void onDialogFinishInitialized();
      }
    
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
        DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                R.style.MyDialogTheme, this, year, month, day);
    
        // you need to inform the activity when Dialog has finish
        // initialized.
        mListener.onDialogFinishInitialized();
    
        return dpd;
      }
    
      ...
    
      // Need to add the activity as the listener.
      @Override 
      public void onAttach(Activity activity) {
        super.onAttach(activity);
        // This makes sure that the container activity has implemented
        // the listener interface. If not, it throws an exception
        try {
          mListener = (DatePickerListener) activity;
        } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString() + " must implement DatePickerListener");
        }
      }
    
      ...
    
    }
    
  2. 实现接口在你 Activity 和调用 getFormattedDate() 方法︰

    public class Booking_activity extends AppCompatActivity
    implements View.OnClickListener, DatePickerFragment.DatePickerListener {
      ...
      private DatePickerFragment dFragment;
      ...
      @Override
      public void onDialogFinishInitialized() {
        // You can call the method here now
        dFragment.getFormattedDate();
      }
      ...
    }
    
赞助商