如何在嵌套的Dialog片段

时间:2017-08-17 05:00:50

标签: android android-fragments dialog

我有一个包含六个按钮的对话框片段。当用户点击按钮时,颜色选择器打开。然后颜色数据传递到第一个对话框片段,并在按钮上设置单独的颜色。 Dialog Fragment 1 。 问题是当我声明接口方法时,我能够获取片段或活动中的数据但不能将数据获取到对话框片段1。 如何将对话框片段2中的数据传递给对话框片段1?

我想将touchRGB颜色传递给第一个对话框片段。

是否可以使用颜色模型中的getter和setter发送数据,如果是的话怎么办?

    builder.setView(layout)
            .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            ColorDialog.this.getDialog().cancel();
                            mListener.onDialogNegativeClick(ColorDialog.this);//calling the listener 

                        }
                    })
            .setPositiveButton(R.string.ok,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            ColorModel colormodel = new ColorModel();
                            colormodel.setColorData(ColorData);
                            mListener.onDialogPositiveClick(ColorDialog.this);
                            mListener.receiveColorData(touchedRGB); //calling the listener 
                        }
                    });
    return builder.create();
}

Dialog Fragment 1

public class addMain extends DialogFragment implements View.OnClickListener,ColorDialog.ColorDialogListener {

    Button  color1,color2,color3,color4,color5,color6,canceladd,okadd;
    SeekBar seek = null;
    public int touchedRGB = 000000;
    Integer[] coloranimchange={0,0,0,0,0,0},customvisibility={0,0,0,0,0,0};
    Button scenes;
    EditText custtomname;
    String theme;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.ColoePickerDialogStyle);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View layout = inflater.inflate(R.layout.dialogadd, null);


        color1 = (Button) layout.findViewById(R.id.addcolor1);
        color2 = (Button) layout.findViewById(R.id.addcolor2);
        color3 = (Button) layout.findViewById(R.id.addcolor3);
        color4 = (Button) layout.findViewById(R.id.addcolor4);
        color5 = (Button) layout.findViewById(R.id.addcolor5);
        color6 = (Button) layout.findViewById(R.id.addcolor6);
        custtomname = (EditText) layout.findViewById(R.id.themename);

        color1.setOnClickListener(this);
        color2.setOnClickListener(this);
        color3.setOnClickListener(this);
        color4.setOnClickListener(this);
        color5.setOnClickListener(this);
        color6.setOnClickListener(this);

        builder.setView(layout)
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                addMain.this.getDialog().cancel();

                            }
                        })
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        });
        return builder.create();

    }


    public static String[] getArray(String input) {
        return input.split("\\|\\$\\|SEPARATOR\\|\\$\\|");
    }

    @Override
    public void onClick(View view) {
        ColorDialog dialogFragment = new ColorDialog();
        dialogFragment.show(getChildFragmentManager(), "ColorSelect");
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {

    }

    @Override
    public void receiveColorData(int ColorData) {
        String col = String.valueOf(ColorData);
        Log.v("col",col);
    }
}

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您只是实现了ColorDialog.ColorDialogListener,但是您忘记将回调设置为ColorDialog。

对话片段1

ColorDialog dialogFragment = new ColorDialog();
dialogFragment.setCallBack(this);
dialogFragment.show(getChildFragmentManager(), "ColorSelect");

ColorDialog

public ColorDialog extends DialogFragment {
   private ColorDialogListener mCallback;

   public void setCallBack(ColorDialogListener listener) {
       this.mCallback = listener;
   }

   public interface ColorDialogListener {
          public void onColorSelect(ColorModel model);
   }

  /** Now you call back is ready to send data**/
  mCallback.onColorSelect(colorModel )
}