使用 onDismiss() 从 DialogFragment 获取信息

时间:2023-04-24
本文介绍了使用 onDismiss() 从 DialogFragment 获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个应用程序,并且正在使用扩展 DialogFragment 的自定义对话框.此对话框将包含我想传递给父活动的某些字段.我尝试实现 OnDismissListener 但参数是对话框接口.

I am working on an app and I am using a custom dialog which extends DialogFragment. This dialog will contain certain field that I want to pass to the parent activity. I tried implementing OnDismissListener but the parameter is a Dialog Interface.

有什么想法吗?

父活动:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BreakCreator mDialog = new BreakCreator();
            mDialog.show(getSupportFragmentManager(), "start break Creator");

        }
    });

听众:

@Override
public void onDismiss(DialogInterface dialog) {
    Log.d("debug", "in onDismiss");
        BreakCreator mBreakCreator = BreakCreator.class.cast(dialog);// This MIGHT not work
    //TODO cast and shit

        if(!mBreakCreator.isCancelled() ){
            int startMinute = mBreakCreator.getStartMinute();
            int startHour = mBreakCreator.getStartHour();
            int endMinute = mBreakCreator.getEndMinute();
            int endHour = mBreakCreator.getEndHour();
            String day = mBreakCreator.getDay();

            Break mBreak = new Break(new ultramirinc.champs_mood.Time(startHour, startMinute),
                    new ultramirinc.champs_mood.Time(endHour, endMinute), day);
            breakList.add(mBreak);
            Log.d("created", "break added");
            recyclerView.invalidate();

        }else{
            Log.d("debug", "is not cancelled");
    }
}

对话框类:

public void onDismiss(final DialogInterface dialog) {
    super.onDismiss(dialog);
    final Activity activity = getActivity();
    if (activity instanceof DialogInterface.OnDismissListener) {
        ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
    }
}

推荐答案

使用自定义监听器,下面是如何实现的示例.Android 开发者指南 中也对此进行了说明.p>

Use a custom listener, below is an example on how this could be implemented. This is also explained in the Android Developer Guide.

public class CustomDialog extends DialogFragment {

   public interface CustomListener{
      void onMyCustomAction(CustomObject co);
   }

   private CustomListener mListener;

   public void setMyCustomListener(CustomListener listener){
     mListener = listener;
   }

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
      ...
      Code to create dialog 
      ...
   }

   @Override
   public void onDismiss(DialogInterface dialog) {
       if(mListener != null){
          CustomObject o = new CustomObject();
          mListener.onMyCustomAction(o);
       }
       super.onDismiss();
   }
}

并在创建自定义对话框时,设置监听器.

And when the custom dialog is created, set the listener.

CustomDialog awesomeDialog = new CustomDialog();
awesomeDialog.setMyCustomListener(new CustomDialog.CustomListener() {
  @Override
  public void onMyCustomAction(CustomObject o){
     Log.i("TAG",o.toString());
  }
});

这篇关于使用 onDismiss() 从 DialogFragment 获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Android:对话框在 droid 3 上有额外的、丑陋的空间? 下一篇:AlertDialog.show 在服务中被忽略

相关文章