按下警报对话框按钮总是返回 0 值

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


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 值,当我再次打开我们的警报对话框时,它返回正确的值。


慕慕森
浏览 148回答 2
2回答

繁花如伊

您总能获得BTN_PRESSED与0价值,只要你在你的实例化AlertUtils对象和调用ShowAlertWithTwoButtons方法。但是如果你ShowAlertWithTwoButtons再次回忆,你会得到另一个值。我认为您目前正在做的事情如下:// First, you're instantiating the objectAlertUtils alertUtils = new AlertUtils(getContext());// then you're calling the methodint pressedButton = alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");// which will return pressedButton as 0// then you calling the method again after clicked yes or noint 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 methodalertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");

青春有我

用这种方法试试。像这样制作 AlertUtils 类。public class AlertUtils {private AlertDialog.Builder builder;private AlertDialogListener alertDialogListener;// Interface to send back the response of clickinterface 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");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java