本文介绍了对话框泛型函数 - 返回布尔根据用户按键preSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,将显示一个警告对话框,它返回一个基于按钮布尔点击

I would like to write a function that would show a alert dialogue box which returns boolean based on the button clicked

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变量,因为它不是最终的。但是,使其最终并没有为我服务我的目的。

I have written the above function but the problem is, Inner class will not be able to access the returnValue variable as it is not final. But making it final does not serve me my purpose.

我是一个C#开发人员,我想实现的东西像下面的android

I am a c# developer and I am trying to achieve something like below in android

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

        return result;
    } 

任何帮助将AP preciated

Any help will be appreciated

推荐答案

完整的解决方案试试这个

1)Createa接口

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) {

                    }
                });

这篇关于对话框泛型函数 - 返回布尔根据用户按键preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:43