java-在OnClickListener中实现DialogFragment接口

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

概述我需要构建一个DialogFragment,它将用户输入从对话框返回到活动.该对话框需要在OnClickListener中调用,当单击列表视图中的元素时会调用该对话框.DialogFragment的返回值(用户的输入)应该在活动的OnClickListener中直接可用.我尝试通过遵循官方文档来实现此目的:http://developer.

我需要构建一个DialogFragment,它将用户输入从对话框返回到活动.
该对话框需要在OnClickListener中调用,当单击列表视图中的元素时会调用该对话框.
DialogFragment的返回值(用户的输入)应该在活动的OnClickListener中直接可用.

我尝试通过遵循官方文档来实现此目的:http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

我需要像下面这样的东西,因为我不知道如何使匿名的OnClickListener实现CustomNumberPicker类的接口,所以它不起作用.
据我所知,实现接口是必要的,以便将DialogFragment中的数据返回给Activity.

主要活动:

public class MainAcitivity extends ActionBaractivity {    [...]    // ArrayAdapter of the ListvIEw    private class ListVIEwArrayAdapter extends ArrayAdapter<Exercise> {        public ListVIEwArrayAdapter(Context context, ArrayList<Exercise> exercises) {            super(context, 0, exercises);        }        @OverrIDe        public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {            [...]            if (convertVIEw == null) {                convertVIEw = LayoutInflater.from(getContext()).inflate(R.layout.item_workoutdetail, parent, false);            }            TextVIEw tvSets = (TextVIEw) convertVIEw.findVIEwByID(R.ID.tvWorkoutExerciseSets);            tvSets.setText(sets.toString());            // OnClickListener for every element in the ListVIEw            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();                    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            });            return convertVIEw;        }    }}

CustomNumberPicker(基本上与文档中的相同):

public class CustomNumberPicker extends DialogFragment {    public interface NoticeDialogListener {        public voID onDialogPositiveClick(DialogFragment dialog);        public voID onDialogNegativeClick(DialogFragment dialog);    }    // 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);        // Verify that the host activity implements the callback interface        try {            // Instantiate the NoticeDialogListener so we can send events to the host            mListener = (NoticeDialogListener) activity;        } catch (ClassCastException e) {            // The activity doesn't implement the interface, throw exception            throw new ClassCastException(activity.toString()                + " must implement NoticeDialogListener");        }    }    @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) {                        // Return stuff here to the activity?                    }                })                .setNegativebutton("cancle", new DialogInterface.OnClickListener() {                    public voID onClick(DialogInterface dialog, int ID) {                        // User cancelled the dialog                    }                });        // Create the AlertDialog object and return it        return builder.create();    }}

解决方法:

像这样吗

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    }

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

总结

以上是内存溢出为你收集整理的java-在OnClickListener中实现DialogFragment接口全部内容,希望文章能够帮你解决java-在OnClickListener中实现DialogFragment接口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: http://outofmemory.cn/web/1094362.html

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

发表评论

登录后才能评论

评论列表(0条)

保存