【问题标题】:[Android]Alertdialog return boolean onclick[Android]Alertdialog 返回 boolean onclick
【发布时间】:2011-10-10 22:40:41
【问题描述】:

我有一个 AlertDialog 的问题:我希望我的 AlertDialog 在用户单击正按钮时返回 true,如果他单击负按钮则返回 false。该函数必须阻止程序,直到用户单击按钮。

我的代码:

public static boolean errorMD5(Context context){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(context.getString(R.string.error));
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setMessage(R.string.errorMD5);
    builder.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            //The function return true
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {         
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
               //The function return false                    
            }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

【问题讨论】:

    标签: android function dialog return alert


    【解决方案1】:

    在您的 Activity 中,您应该实现 2 个功能。

     onYesPressed() and onNoPressed()
    

    并从您的对话框中调用它们。

    或者你可以只实现一个带有布尔参数的函数

        onUserDismissDialog(boolean allow)
    

    并从您的对话框 OnClickListener 中调用此函数。

    【讨论】:

    • 不,它不起作用我无法在我的活动中调用该函数中的函数(该函数在另一个类中)
    • 你需要调用 MyActivity.this.onUserDismissDialog(true) 。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多