在OnClickListener中实现DialogFragment接口

在OnClickListener中实现DialogFragment接口,第1张

在OnClickListener中实现DialogFragment接口

像这样吗

public class CustomNumberPicker extends DialogFragment {    private NoticeDialogListener ndl;    public interface NoticeDialogListener {        public void onDialogPositiveClick(DialogFragment dialog);        public void onDialogNegativeClick(DialogFragment dialog);    }    //add a custom constructor so that you have an initialised NoticeDialogListener    public CustomNumberPicker(NoticeDialogListener ndl){        super(); this.ndl=ndl;    }    //make sure you maintain an empty constructor    public CustomNumberPicker( ){        super();    }    // Use this instance of the interface to deliver action events    NoticeDialogListener mListener;    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener    @Override    public void onAttach(Activity activity) {        super.onAttach(activity);        //remove the check that verfis if your activity has the DialogListener Attached because you want to attach it into your list view onClick()    }    @Override    public Dialog onCreateDialog(Bundle savedInstanceState) {        // Use the Builder class for convenient dialog construction        alertDialog.Builder builder = new alertDialog.Builder(getActivity());        builder.setMessage("Sets") .setPositiveButton("set", new DialogInterface.onClickListener() {         public void onClick(DialogInterface dialog, int id) {  ndl.onDialogPositiveClick(dialog);         }     })     .setNegativeButton("cancle", new DialogInterface.onClickListener() {         public void onClick(DialogInterface dialog, int id) { ndl.onDialogNegativeClick(dialog);         }     });        // Create the alertDialog object and return it        return builder.create();    }}

然后您的listView onClick变为:

tvSets.setonClickListener(new View.onClickListener() {     @Override     public void onClick(View v) {         // This is where the Dialog should be called and         // the user input from the Dialog should be returned         //          //         DialogFragment numberpicker = new CustomNumberPicker(new NoticeDialogListener() { @Override public void onDialogPositiveClick(DialogFragment dialog) {     //What you want to do incase of positive click } @Override public void onDialogNegativeClick(DialogFragment dialog) {    //What you want to do incase of negative click }        };);         numberpicker.show(MainActivity.this.getSupportFragmentManager(), "NoticeDialogFragment");     }     // Here I would like to implement the interface of CustomNumberPicker     // in order to get the user input entered in the Dialog });

请阅读我添加的注释。由于您确实不需要整个对话框实例来获取所需的值,因此甚至可以对其进行进一步优化

编辑 可能的优化可能是:

侦听器界面更改为:

public interface NoticeDialogListener {        public void onDialogPositiveClick(String output);        public void onDialogNegativeClick(String output);       //or whatever form of output that you want    }

然后相应地修改已实现的方法。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5094666.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存