简体   繁体   English

在Activity中为DialogFragment设置侦听器

[英]Setting a listener in Activity for DialogFragment

I am trying to implement a listener in my Activity on my DialogFragment( which has 3 numberPicker widget elements) which would be used to set values in textViews of the Activity and I don't want to make this Fragment class an inner class of Activity and set the textview in OnClickListener of the OK button as I will have to make my view static in that case which is not desirable. 我正在尝试在我的DialogFragment(具有3个numberPicker小部件元素)上的Activity中实现一个侦听器,该监听器将用于在Activity的textViews中设置值,并且我不想将此Fragment类设置为Activity的内部类,并且在“确定”按钮的OnClickListener中设置textview,因为在这种情况下,我必须将视图设为静态。 I know there is a listener onValueChange of the NumberPicker class, but how do I set a listener which gets values from the 3 picker elements. 我知道NumberPicker类有一个侦听器onValueChange,但是如何设置一个从3个选择器元素获取值的侦听器。 Any help to implement such a listener in the activity would be appreciated. 在活动中实现此类侦听器的任何帮助将不胜感激。

 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();
        }




    }

You can create a Listener interface that is implemented in your Activity and used in DialogFragment through a reference to the Activity. 您可以创建一个Listener接口,该接口在您的Activity中实现,并通过对Activity的引用在DialogFragment使用。 Something like: 就像是:

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

The interface: 界面:

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

In your Fragment: 在您的片段中:

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();
            }
        });

        ...
    }




}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM