单个按钮上的多个onClickListener

时间:2018-03-03 07:13:58

标签: java android

在我的项目中,我必须在应用程序的不同部分使用对话框,并且缺点是用于创建和显示每次使用的Dialog的相同代码!所以我决定创建一个创建和显示对话框的功能,无论我需要什么,我只需要调用它,

问题是,我的Dialog上有2个按钮,在重构onClickListener之前我可以轻松使用

dialog.dismiss();
// more codes ...
setOnClickListener中的

但现在使用传入的OnClickListener对象,我无法控制对话框实例... 这是我写的功能

    public static void warningAndErrorDialog(Activity activity, int titleResourceId, int iconResourceId, int contentResourceId
        , HashMap<CustomDialogButton, View.OnClickListener> buttons) {

    Typeface iranSansFont = setFont(activity, FontStyle.IRAN_SANS_REGULAR);
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog);

    CustomFontTextView cftvTitle = (CustomFontTextView)
            dialog.findViewById(R.id.txtViwDialogTitle);
    if(activity.getString(titleResourceId) != null)
         cftvTitle.setText(titleResourceId);
    else cftvTitle.setText(" ");

    CustomFontTextView cftvContent = (CustomFontTextView)
            dialog.findViewById(R.id.txtViwDialogContent);
    if(activity.getString(contentResourceId) != null)
        cftvContent.setText(contentResourceId);
    else cftvTitle.setText(" ");

    ImageView imgViwDialogTitle = dialog.findViewById(R.id.imgViwDialogTitle);
    imgViwDialogTitle.setImageResource(iconResourceId);


    Button btnYes = null;
    Button btnNo = null;

    for (Map.Entry<CustomDialogButton, View.OnClickListener> button : buttons.entrySet())
        switch (button.getKey()) {
            case YES:
                if (btnYes != null) break;
                btnYes = dialog.findViewById(R.id.btnYes);
                btnYes.setTypeface(iranSansFont);
                if (button.getValue() != null)
                    btnYes.setOnClickListener(button.getValue());
                break;
            case NO:
                if (btnNo != null) break;
                btnNo = dialog.findViewById(R.id.btnNo);
                if (button.getValue() != null) {
                    btnNo.setTypeface(iranSansFont);
                    btnNo.setOnClickListener(button.getValue());
                } else
                    btnNo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });
                break;
            case OK:
                btnYes = dialog.findViewById(R.id.btnYes);
                btnYes.setText(R.string.ok);
                if (button.getValue() != null) btnYes.setOnClickListener(button.getValue());
                else
                    btnYes.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });
                btnNo = dialog.findViewById(R.id.btnNo);
                btnNo.setVisibility(View.INVISIBLE);
                return;
        }


        dialog.show();
}

案件通知是: 我从传递给函数的onClickListener中设置了按钮setOnClickListener。 所以我无法控制其内容。 now如何添加语句dialog.dismiss(); ? 我唯一能想到的是找到一种方法将OnClickListener数组传递给setOnCLickListener方法。

一个用于解除对话框,另一个用于设置按钮的实际工作...

ps:我尝试使用setOnTouchListener解除对话框,但正如我所料,这不起作用......: - ?

那我该怎么办?

最终编辑: 有人建议

btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (View.OnClickListener callback : callbacks)
                    if (callback != null)
                        callback.onClick(view);
            }
        });

(回调是View.OnClickListener对象的List),这就是&#34;如何在单个Button上添加多个onClickListener的确切答案&#34;问题

但@greenapps解决方案是最适合我需要的解决方案,谢谢:)

所以我分享了可能涉及的代码的最终版本:D:

公共类CustomDialog {

private final Dialog dialog;
private Typeface font;
private CustomFontTextView cftvTitle;
private CustomFontTextView cftvContent;
private ImageView imgViwDialogTitle;
private Activity activity;
private Button btnYes;
private Button btnNo;


public CustomDialog(final Activity activity, int titleResourceId, int iconResourceId, int contentResourceId
        , HashMap<CustomDialogButton, View.OnClickListener> buttons) {

    font = Utility.setFont(activity, FontStyle.IRAN_SANS_REGULAR);
    this.activity = activity;
    dialog = new Dialog(this.activity);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog);

    setTitleTextbyId(titleResourceId);
    setContentTextById(contentResourceId);
    setTitleIconById(iconResourceId);

    setButtons(buttons);

}

public CustomDialog(Activity activity) {
    font = Utility.setFont(activity, FontStyle.IRAN_SANS_REGULAR);
    this.activity = activity;
    dialog = new Dialog(this.activity);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog);

    setTitleTextbyId(0);
    setContentTextById(0);
    setTitleIconById(0);

    setButtons(null);
}

public void show() {
    dialog.show();
}

public void setButtons(HashMap<CustomDialogButton, View.OnClickListener> buttons) {

    for (final Map.Entry<CustomDialogButton, View.OnClickListener> button : buttons.entrySet())
        switch (button.getKey()) {
            case YES:
                setButtonYes(button);
                break;
            case NO:
                setButtonNo(button);
                break;
            case OK:
                setButtonYes(button);
                btnYes.setText(R.string.ok);
                btnNo = dialog.findViewById(R.id.btnNo);
                btnNo.setVisibility(View.INVISIBLE);
                break;
        }
}

@NonNull
private void setButtonNo(final Map.Entry<CustomDialogButton, View.OnClickListener> button) {

    if (btnNo != null) return;
    btnNo = dialog.findViewById(R.id.btnNo);
        btnNo.setTypeface(font);
        btnNo.setOnClickListener(button.getValue());
        btnNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                    if(button.getValue() != null)
                        button.getValue().onClick(view);
            }
        });
}

@NonNull
private void setButtonYes(final Map.Entry<CustomDialogButton, View.OnClickListener> button) {


    if (btnYes != null) return;
    btnYes = dialog.findViewById(R.id.btnYes);
    btnYes.setTypeface(font);
        btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                if(button.getValue() != null)
                    button.getValue().onClick(view);
            }
        });
}

public void setTitleIconById(int iconResourceId) {
    this.imgViwDialogTitle = dialog.findViewById(R.id.imgViwDialogTitle);
    if (activity.getResources().getDrawable(iconResourceId) != null)
        imgViwDialogTitle.setImageResource(iconResourceId);
}

public void setContentTextById(int contentResourceId) {
    this.cftvContent = (CustomFontTextView)
            this.dialog.findViewById(R.id.txtViwDialogContent);
    if (this.activity.getString(contentResourceId) != null)
        cftvContent.setText(contentResourceId);
    else cftvTitle.setText(" ");
}

public void setTitleTextbyId(int titleResourceId) {
    this.cftvTitle = (CustomFontTextView)
            this.dialog.findViewById(R.id.txtViwDialogTitle);
    if (this.activity.getString(titleResourceId) != null)
        cftvTitle.setText(titleResourceId);
    else cftvTitle.setText(" ");
}

public void dismiss() {
    dialog.dismiss();
}

}

1 个答案:

答案 0 :(得分:0)

btnYes.setOnClickListener(button.getValue());

更改为:

btnYes.setOnClickListener(new View.OnClickListener() { 
   @Override public void onClick(View view) { 
       dialog.dismiss(); 
       button.getValue().onClick(view); 
   }
});