Dialogs generic function - return boolean based on user button press

一世执手 提交于 2019-12-06 04:53:17

问题


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;
}

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.

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;
    } 

Any help will be appreciated


回答1:


Complete solution Try this

1) Createa Interface

import android.content.DialogInterface;

public interface AlertMagnatic {

    public abstract void onButtonClicked(boolean value);

}

2) Generalize method for confirm dialog.

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) How to use

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

                    }
                });


来源:https://stackoverflow.com/questions/18842277/dialogs-generic-function-return-boolean-based-on-user-button-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!