【问题标题】:Dialogs generic function - return boolean based on user button press对话框通用函数 - 基于用户按钮按下返回布尔值
【发布时间】:2013-09-17 05:59:12
【问题描述】:

我想编写一个函数来显示一个警报对话框,该对话框根据单击的按钮返回布尔值

private Boolean ShowWarningMessageBox(String Title, String message)
{
    boolean returnValue = false;

    AlertDialog.Builder builder = new AlertDialog.Builder(
            getApplicationContext());

    builder.setTitle(Title);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {
            returnValue = true;
        }
    });

    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    dialog.cancel();
                    returnValue = false;
                }
            });

    builder.show();

    return returnValue;
}

我已经编写了上述函数,但问题是,内部类将无法访问 returnValue 变量,因为它不是最终的。但最终确定并不能满足我的目的。

我是一名 c# 开发人员,我正在尝试在 android 中实现以下目标

private DialogResult ShowWarningMessageBox(string errorMessage)
    {
        DialogResult result = MessageBox.Show(errorMessage,
                 Resources.WarningCaption.ToString(),
                 MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk,
                 MessageBoxDefaultButton.Button1);

        return result;
    } 

任何帮助将不胜感激

【问题讨论】:

  • 你需要为此使用接口
  • 即使你可以访问returnValue,每次都是false。因为它是在显示对话框后返回的,而不是在单击按钮时返回的。如果它会等到按钮被点击,你会得到一个 ANR,因为线程休眠直到按钮被点击。

标签: android android-dialog


【解决方案1】:

完整的解决方案试试这个

1) 创建界面

import android.content.DialogInterface;

public interface AlertMagnatic {

    public abstract void onButtonClicked(boolean value);

}

2) 确认对话框的泛化方法。

public static void getConfirmDialog(final Context mContext,
            final String title, final String msg,
            final String positiveBtnCaption, final String negativeBtnCaption,
            final boolean isCancelable, final AlertMagnatic target) {

        ((Activity) mContext).runOnUiThread(new Runnable() {

            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

                int imageResource = android.R.drawable.ic_dialog_alert;
                Drawable image = mContext.getResources().getDrawable(
                        imageResource);

                builder.setTitle(title)
                        .setMessage(msg)
                        .setIcon(image)
                        .setCancelable(false)
                        .setPositiveButton(positiveBtnCaption,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        target.onButtonClicked(true);
                                    }
                                })
                        .setNegativeButton(negativeBtnCaption,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        target.onButtonClicked(false);
                                    }
                                });

                AlertDialog alert = builder.create();
                alert.setCancelable(isCancelable);
                alert.show();
                if (isCancelable) {
                    alert.setOnCancelListener(new OnCancelListener() {

                        @Override
                        public void onCancel(DialogInterface arg0) {
                            target.onButtonClicked(false);
                        }
                    });
                }
            }
        });

    }

3) 使用方法

getConfirmDialog(getString(R.string.logout), getString(R.string.logout_message), getString(R.string.yes), getString(R.string.no), false,
                new AlertMagnatic() {


                    @Override
                    public void onButtonClicked(boolean value) {

                    }
                });

【讨论】:

  • 非常感谢您的回复。我在 alert.show() 处收到错误;和日志显示粘贴在这里pastebin.com/VXreEWef
  • 您从哪里调用此对话框?
  • 不要通过 getApplicationContext() 使用 ActivityName.this 而是编辑方法更新并检查
  • 这太棒了!为什么 Android API 中没有类似的功能!?
  • 如果其他人也想要这个:添加“TextView messageText = (TextView)alert.findViewById(android.R.id.message); messageText.setGravity(Gravity.CENTER);”下面 alert.show();使文本居中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2012-05-03
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
相关资源
最近更新 更多