Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按下警报对话框按钮始终返回0值_Java_Android_Android Alertdialog - Fatal编程技术网

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

Java 按下警报对话框按钮始终返回0值,java,android,android-alertdialog,Java,Android,Android Alertdialog,所以,我想在弹出警报对话框时检测用户按下的按钮。这是我的密码 public class AlertUtils { private int BTN_PRESSED; private AlertDialog.Builder builder; public AlertUtils(Context context){ builder = new AlertDialog.Builder(context); } public int ShowAl

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

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

每当你实例化
AlertUtils
对象并用两个按钮调用
ShowAlertWith
方法。但是,如果您再次使用两个按钮调用
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添加一个监听器(哦,不,另一个监听器)


更新

您需要为click button添加另一个侦听器,如下所示:

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");

每当实例化
AlertUtils
对象和调用
ShowAlertWithTwoButtons
方法时,总是会用
0
值按下
BTN\u。但是,如果您再次使用两个按钮调用
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添加一个监听器(哦,不,另一个监听器)


更新

您需要为click button添加另一个侦听器,如下所示:

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");

这样试试看。将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");

这样试试看。将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");

你做错了。阅读有关@AliAhmed的更多信息,其返回类型为void。您的方法返回默认值,因为您不等待单击。你可以自己调试。为什么这个问题被否决了?如果他做错了什么,或者理解错了什么,我们应该纠正他,而不是投票否决问题投票否决一个问题可能会阻止问题的所有者再次提问,!你做错了。阅读有关@AliAhmed的更多信息,其返回类型为void。您的方法返回默认值,因为您不等待单击。你可以自己调试。为什么这个问题被否决了?如果他做错了什么,或者理解错了什么,我们应该纠正他,而不是投票否决问题投票否决一个问题可能会阻止问题的所有者再次提问,!你明白我的意思了,但我想从onclicklisteners返回值?嘿,伙计,谢谢你的回答,尽管没有必要从函数返回值,因为我们已经通过回调方法返回了值。如果你能编辑这个部分,也会帮助其他人使用这个答案。再次感谢。谢谢你的帮助。不客气;)。你对返回值的看法是正确的,我将更新答案。你明白我的意思了,但我想从onclicklisteners返回值?嘿,伙计,谢谢你的回答,尽管没有必要从函数返回值,因为我们已经通过回调方法返回了值。如果你能编辑这个部分,也会帮助其他人使用这个答案。再次感谢。谢谢你的帮助。不客气;)。返回值正确,我将更新答案。