处理button在DialogFragment中单击

我有扩展DialogFragment的片段,我有一个自定义的布局,它包含两个edittexts和两个button – 确定和取消。 我的对话框显示得很好,使用onCreateView方法来指定布局,但我不知道如何处理button点击。 在onCreateView方法中,button.setOnClickListener不起作用。 这可能有一个简单的解决scheme,但我坚持了几个小时。 我非常感谢一个build议或示例代码。

PS我不想使用AlertDialog,因为在这种情况下,当点击okbutton时,对话框会自动closures,而且我不能对edittext进行validation(例如:当用户按下okbutton,edittext是空的我不希望对话框消失)。 这就是我select创build自定义对话框并轻松pipe理button行为的方法。

Solutions Collecting From Web of "处理button在DialogFragment中单击"

按照这些路线行事

Dialog dl = mDialogFragment.getDialog(); Button btn = dl.findViewbyid(R.id.btn); btn.setOnclicklistener(this); 

这是我正在使用的对话框的代码(对话框的实际GUI在布局资源confirm_dialog.xml中定义):

 public class ConfirmDialog extends DialogFragment { public static String TAG = "Confirm Dialog"; public interface ConfirmDialogCompliant { public void doOkConfirmClick(); public void doCancelConfirmClick(); } private ConfirmDialogCompliant caller; private String message; public ConfirmDialog(ConfirmDialogCompliant caller, String message){ super(); this.caller = caller; this.message = message; } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.confirm_dialog, container, false); getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); ((TextView) view.findViewById(R.id.textview_confirm)).setText(message); ((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() { public void onClick(View v) { // When button is clicked, call up to owning activity. caller.doOkConfirmClick(); } }); ((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() { public void onClick(View v) { // When button is clicked, call up to owning activity. caller.doCancelConfirmClick(); } }); return view; } } 

该对话框使用以下几行创build

 confirm_dialog = new ConfirmDialog(this, message); confirm_dialog.show(getActivity().getSupportFragmentManager(), ConfirmDialog.TAG); 

接口定义用于确保调用者(Fragment或者Activity)实现处理由控制器抛出的事件的方法。 也就是说,调用这个对话框的Fragment或者Activity必须实现给定的接口。
也许有一个更好的解决scheme,但这是我想出的。 希望能帮助到你!

这里是一个例子来说明从FragmentDialog类的对话框中点击取消button:

我用android.support.v4.app.DialogFragment;

 public class MyDialogFragment extends DialogFragment { public MyDialogFragment(){} public static String TAG = "info Dialog"; Button btn; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.info_layout, container); getDialog().requestWindowFeature(STYLE_NO_TITLE); btn=(Button)view.findViewById(R.id.close_dialog_btn_info_layout); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub getDialog().dismiss(); } }); return view; } } 

另一种select是让您的自定义DialogFragment类实现OnClickListener 。 然后,您只需设置setOnClickListener ,查看您想要处理的点击次数,并捕获onClick的点击次数。

 // 1. implement OnClickListener public class MyDialogFragment extends DialogFragment implements View.OnClickListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.my_dialog_layout, null); // 2. set click listeners on desired views view.findViewById(R.id.my_view_1).setOnClickListener(this); view.findViewById(R.id.my_view_2).setOnClickListener(this); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view) // ... return builder.create(); } // 3. capture the clicks and respond depending on which view @Override public void onClick(View view) { switch (view.getId()) { case R.id.my_view_1: // do something break; case R.id.my_view_2: // do something break; default: break; } } } 

它很容易在活动,但在DialogFragment我们做更多的代码。

在这里,我如何解决我的问题,在DialogFragment类中做你的芦丁findView

 btn_ocak = (Button) view.findViewById(R.id.btn_cal_ocak); btn_subat = (Button) view.findViewById(R.id.btn_cal_subat); btn_mart = (Button) view.findViewById(R.id.btn_cal_mart); btn_nisan = (Button) view.findViewById(R.id.btn_cal_nisan); btn_ocak.setOnClickListener(this); btn_subat.setOnClickListener(this); btn_mart.setOnClickListener(this); btn_nisan.setOnClickListener(this); 

实施onClick OnClickListener到你的class级

 public class CalendarPopUp extends DialogFragment implements View.OnClickListener 

并在onClick方法中做你想做的事情,通过这些我们激活了我们视图的onClick事件

  @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_cal_ocak: seciliAy = "Ocak"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_subat: seciliAy = "Subat"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_mart: seciliAy = "Mart"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_nisan: seciliAy = "Nisan"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_mayis: seciliAy = "Mayıs"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_haziran: seciliAy = "Haziran"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_temmuz: seciliAy = "Temmuz"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_agustos: seciliAy = "Agustos"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_eylul: seciliAy = "Eylül"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_ekim: seciliAy = "Ekim"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_kasim: seciliAy = "Kasım"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; case R.id.btn_cal_aralik: seciliAy = "Aralık"; setMonthOnShare(seciliAy); mCallback.onSelectedData(seciliAy); dismiss(); break; default: break; } } 

如果你想知道如何传递值,请按照这些步骤clike_here