按下警报对话框按钮始终返回0值

时间:2018-10-22 09:07:50

标签: java android alertdialog

因此,我想检测警报对话框弹出时用户按下的按钮。这是我的代码。

public class AlertUtils {

    private int BTN_PRESSED;

    private AlertDialog.Builder builder;

    public AlertUtils(Context context){
        builder = new AlertDialog.Builder(context);
    }

    public int ShowAlertWithTwoButtons(String Title,String Message,String PositiveButtonText,
                                        String NegativeButtonText){
        builder.setTitle(Title);
        builder.setMessage(Message);

        builder.setPositiveButton(PositiveButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                BTN_PRESSED = i;
            }
        });

        builder.setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                BTN_PRESSED = i;
                dialogInterface.dismiss();
            }
        });

        builder.show();
        return BTN_PRESSED;
    }
}

通过调用ShowAlertWithTwoButtons方法,返回检测到正或负按钮按下的int值。我的问题是,当我从警报对话框中选择时,它给我默认的0值,当我再次打开警报对话框时,它返回正确的值。

2 个答案:

答案 0 :(得分:2)

以这种方式尝试。像这样使AlertUtils类。

public class AlertUtils {
private AlertDialog.Builder builder;
private AlertDialogListener alertDialogListener;

// Interface to send back the response of click
interface AlertDialogListener {
    void onClick(int a);
}

public AlertUtils(Context context, AlertDialogListener alertDialogListener) {
    builder = new AlertDialog.Builder(context);
    this.alertDialogListener = alertDialogListener;
}

public void ShowAlertWithTwoButtons(String Title, String Message, String PositiveButtonText,
                                    String NegativeButtonText) {
    builder.setTitle(Title);
    builder.setMessage(Message);
    builder.setPositiveButton(PositiveButtonText, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // if you want to pass the actual value of i,then pass the i in onClick or if you want 1 on 
            // positive button click then pass 1 here.
            alertDialogListener.onClick(1);
        }
    });
    builder.setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // if you want to pass the actual value of i, then pass the i in onClick or if you want 1 on 
            // negative button click then pass 0 here.
            alertDialogListener.onClick(0);
            dialogInterface.dismiss();
        }
    });
    builder.show();
}

}

在需要时以这种方式调用对话框。

  AlertUtils alertUtils = new AlertUtils(getContext(), new AlertUtils.AlertDialogListener() {
        @Override
        public void onClick(int a) {
            if (a == 1) {
                // Do your work on Positive button click
            } else {
                // Do your work on Negative button click
            }
        }
    });
    alertUtils.ShowAlertWithTwoButtons("Alert Dialog", "Alert Dialog Description ", "Positive", "Negative");

答案 1 :(得分:1)

每当实例化BTN_PRESSED对象并调用0方法时,总会得到带有AlertUtils值的ShowAlertWithTwoButtons。但是,如果再次调用ShowAlertWithTwoButtons,则会获得另一个值。

我认为您当前正在做的事情如下:

// First, you're instantiating the object
AlertUtils alertUtils = new AlertUtils(getContext());

// then you're calling the method
int pressedButton = alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");

// which will return pressedButton as 0

// then you calling the method again after clicked yes or no
int anotherPressedButton = alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");

// which will not zero. But can be -1, -2, -3 like in the
// https://developer.android.com/reference/android/content/DialogInterface.html

由于AlertDialog界面的异步性质,如果要在单击后立即获取按钮值,这是不正确的。

相反,您需要在AlertUtils中添加一个侦听器(哦,不,另一个侦听器)。


更新

您需要为单击按钮添加另一个侦听器,如下所示:

public class AlertUtils {

    public interface Listener {
      void onButtonClicked(int pressedButton);
    }

    private Listener mListener;

    private AlertDialog.Builder builder;

    public AlertUtils(Context context, Listener listener){
        builder = new AlertDialog.Builder(context);
        mListener = listener;
    }

    public void ShowAlertWithTwoButtons(String Title,String Message,String PositiveButtonText,
                                        String NegativeButtonText){
        ...

        builder.setPositiveButton(PositiveButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mListener.onButtonClicked(i);
            }
        });

        builder.setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mListener.onButtonClicked(i);
                dialogInterface.dismiss();
            }
        });

        builder.show();
    }
}

然后您可以使用以下方法创建并调用方法:

// create the listener to listen for the clicked button.
AlertUtils.Listener listener = new AlertUtils.Listener() {
      @Override
      public void onButtonClicked(int pressedButton) {
        // here you'll receive the button value
        // do something here.
      }
   };

AlertUtils alertUtils = new AlertUtils(getContext(), listener);

// then you're calling the method
alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");