简体   繁体   English

处理DialogFragment中的按钮单击

[英]handle button clicks in a DialogFragment

I have Fragment that extends DialogFragment and I have a custom layout for it which contains two edittexts and two buttons - ok and cancel. 我有Fragment扩展DialogFragment,我有一个自定义布局,它包含两个edittexts和两个按钮 - 确定和取消。 My dialog displays just fine, using the onCreateView method for specifying the layout, but I don't know how to handle button clicks. 我的对话框显示得很好,使用onCreateView方法指定布局,但我不知道如何处理按钮点击。 Inside the onCreateView method, button.setOnClickListener doesn't work. 在onCreateView方法中,button.setOnClickListener不起作用。 This may have a simple solution, but I am stuck for several hours. 这可能有一个简单的解决方案,但我被困了几个小时。 I would very much appreciate an advice or example code. 我非常感谢建议或示例代码。

PS I don't want to use AlertDialog, because in this case when clicking on the ok button, the dialog automatically dismisses itself, and I can't do a validation on the edittext's (example: when the user presses ok button and the edittext's are empty I don't want the dialog to disappear). PS我不想使用AlertDialog,因为在这种情况下单击ok按钮时,对话框会自动关闭,我无法对edittext进行验证(例如:当用户按下ok按钮和edittext的时候是空的我不希望对话框消失)。 That is way I went with the option for creating a custom dialog and easily manage the buttons behavior. 这就是我选择创建自定义对话框并轻松管理按钮行为的方法。

This is the code for a Dialog I'm using (the actual GUI for the dialog is defined in the layout resource confirm_dialog.xml): 这是我正在使用的Dialog的代码(对话框的实际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;
    }

}

The dialog is created with the following lines 使用以下行创建对话框

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

The interface definition is used to assure that the caller (Fragment or Activity) implements the methods to handle the events thrown by the controller. 接口定义用于确保调用者(Fragment或Activity)实现处理控制器抛出的事件的方法。 That is, a Fragment or Activity calling this dialog must implement the given interface. 也就是说,调用此对话框的Fragment或Activity必须实现给定的接口。
Maybe there is a better solution but this is the one I figured out. 也许有一个更好的解决方案,但这是我想到的。 Hope it helps! 希望能帮助到你!

here is an example to handel a cancel button click on a dialog from the FragmentDialog class: 这是一个例子,来自FragmentDialog类的对话框中的取消按钮单击:

i used android.support.v4.app.DialogFragment; 我使用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;
    }





}

Do along these lines 沿着这些路线

Dialog dl = mDialogFragment.getDialog();
Button btn = dl.findViewById(R.id.btn);
btn.setOnClickListener(this);

Another option is to let your custom DialogFragment class implement OnClickListener . 另一个选择是让您的自定义DialogFragment类实现OnClickListener Then you just setOnClickListener for whatever views you want to handle clicks on and catch the clicks in onClick . 然后,您只需将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;
        }
    }
}

Its easy in activity but in DialogFragment we do some more codes. 它的活动很简单,但在DialogFragment中我们会做更多的代码。

Inside the DialogFragment class do your rutin findView on onActivityCreated method 在DialogFragment类中,在onActivityCreated方法上执行你的rutin 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);

Implement onClick OnClickListener to your class 实现onClick OnClickListener到您的类

public class CalendarPopUp extends DialogFragment implements View.OnClickListener

and do what you want inside onClick method, by doing these we activated the onClick events of our views 并在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;
    }
}

and if you wonder how to pass values follow these step clike_here 如果您想知道如何传递值,请按照以下步骤进行clike_here

or 要么

class MyDialog extends DialogFragment {
   public View.OnClickListener onButtonOk;
   public EditText edit_text;

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
       LayoutInflater li = LayoutInflater.from(builder.getContext());
       View view = li.inflate(R.layout.custom_dialog, null);
       Button buttonOK = view.findViewById(R.id.button_ok);
       edit_text = view.findViewById(R.id.edit_text);
       buttonOk.setOnClickListener(onButtonOk);
       builder.setView(view);

       return builder.create();
   }

}


// use
final MyDialog dialog=new MyDialog();
dialog.onButtonOk=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), dialog.edit_text.getText(), Toast.LENGTH_SHORT).show();
    }
};
dialog.show(getSupportFragmentManager(),null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM