在Activity中为DialogFragment设置侦听器

时间:2019-06-29 21:35:49

标签: java android dialog android-dialogfragment

我试图在我的DialogFragment上的Activity中实现一个侦听器(该监听器具有3个numberPicker小部件元素),该监听器将用于在Activity的textViews中设置值,并且我不想将此Fragment类设为内部类并在“确定”按钮的OnClickListener中设置textview,因为在这种情况下,我必须将视图设为静态,这是不希望的。我知道NumberPicker类有一个侦听器onValueChange,但是如何设置一个从3个选择器元素获取值的侦听器。在活动中实现此类侦听器的任何帮助将不胜感激。

 public class PickerDialog extends DialogFragment {
        NumberPicker numberPicker;
        NumberPicker numberPicker2;
        NumberPicker numberPicker3;
        public static PickerDialog newInstance() {
            PickerDialog frag = new PickerDialog ();
            return frag;
        }


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            View child = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
             numberPicker = child.findViewById(R.id.numberPicker1);
              numberPicker2 = child.findViewById(R.id.numberPicker2);
               numberPicker3 = child.findViewById(R.id.numberPicker3);

            numberPicker.setMinValue(0);
            numberPicker.setMaxValue(59);
            numberPicker3.setMinValue(0);
            numberPicker3.setMaxValue(59);

            numberPicker2.setMinValue(0);
            numberPicker2.setMaxValue(59);

            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(getActivity(), R.style.Theme_Material_Dialog_Alert);


            builder.setTitle("Choose Value");


            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dismiss();
                }
            });

            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();

                }
            });

            builder.setView(child);
            return builder.create();
        }




    }

1 个答案:

答案 0 :(得分:0)

您可以创建一个Listener接口,该接口在Activity中实现,并通过引用Activity在DialogFragment中使用。像这样:

public class MyActivity extends Activity implements NumberPickerListener {
    ...
    @Override
    public void onValuesPicked(int first, int second, int third) {
        //Do work here.
    }
    ...
}

界面:

interface NumberPickerLisener {
    void onValuesPicked(int first, int second, int third);
}

在您的片段中:

public class PickerDialog extends DialogFragment {

    NumberPickerListener listener;

    ...

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View child = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
        listener = (MyActivity)getActivity();

        ...

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onValuesPicked(numberPicker.getValue(), numberPicker2.getValue(), numberPicker3.getValue());
                dismiss();
            }
        });

        ...
    }




}