Android FragmentDialog和接口,如何知道按钮执行了什么按钮

时间:2016-10-27 23:48:07

标签: android android-fragments

我创建了一个DatePicker对话框片段,用于我的片段。

我的片段中有两个按钮。每个按钮点击我得到一个日期。

在我的片段的监听器结果中,我需要知道点击了什么按钮来执行操作,因为我有两个可以调用dialogFragment的按钮。

我怎么知道在onCalendarDialogDateSelected结果事件中点击了哪个按钮?

在我的片段中,我这样做:

public class AgendaMedFragment extends Fragment implements CalendarDialogFragment.CalendarDialogListener {
    .....

    Button but_data_de= (Button) view.findViewById(R.id.but_data_agenda_de);
    but_data_de.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            CalendarDialogFragment myDialogFragment = CalendarDialogFragment
                    .newInstance(year,month,day);
            myDialogFragment.setTargetFragment(AgendaMedFragment.this,0);
            myDialogFragment.show(getFragmentManager(), "myDialogFragment");
        }
    });

    Button but_data_para= (Button) view.findViewById(R.id.but_data_agenda_para);
    but_data_para.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            CalendarDialogFragment myDialogFragment = CalendarDialogFragment
                    .newInstance(year,month,day);
            myDialogFragment.setTargetFragment(AgendaMedFragment.this,0);
            myDialogFragment.show(getFragmentManager(), "myDialogFragment");
        }
    });

    @Override
    public void onCalendarDialogDateSelected(int year, int month, int day) {
        //Here I need to know what button was clicked to execute an action, since I have two buttons that can call the dialogFragment
    }
}

我的dialogFragment是:

public class CalendarDialogFragment extends DialogFragment  implements DatePickerDialog.OnDateSetListener {

    public interface CalendarDialogListener {
        void onCalendarDialogDateSelected(int year, int month, int day);
    }

    // Usa essa instância da interface para entregar eventos de ação
    CalendarDialogListener mListener;

    static CalendarDialogFragment newInstance(int year, int month, int day) {
        CalendarDialogFragment dialog = new CalendarDialogFragment();
        Bundle args = new Bundle();
        args.putInt("year", year);
        args.putInt("month", month);
        args.putInt("day", day);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            mListener = (CalendarDialogListener) getTargetFragment();

        } catch (ClassCastException e) {
            throw new ClassCastException("Calling Fragment must implement CalendarDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int year = getArguments().getInt("year");
        int month = getArguments().getInt("month");
        int day= getArguments().getInt("day");
        // Create a new instance of DatePickerDialog and return it
        Dialog myDialog = new DatePickerDialog(getActivity(), this, year, month, day);
        return myDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        mListener.onCalendarDialogDateSelected(year, month, day);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以再向DialogFragment的构造函数发送一个参数,该参数对应于单击的按钮。

示例: 如果是button1,则将此作为另一个参数发送到dialogFragment构造函数,并在代码中使用它。