栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在OnClickListener中实现DialogFragment接口

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在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    }

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428762.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号