android – Dialogs泛型函数 – 根据用户按钮返回布尔值

android – Dialogs泛型函数 – 根据用户按钮返回布尔值,第1张

概述我想写一个函数,它会显示一个警告对话框,根据点击的按钮返回布尔值 private Boolean ShowWarningMessageBox(String Title, String message){ boolean returnValue = false; AlertDialog.Builder builder = new AlertDialog.Builder( 我想写一个函数,它会显示一个警告对话框,根据点击的按钮返回布尔值

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

我已经编写了上面的函数,但问题是,Inner类将无法访问returnValue变量,因为它不是final.但是让它成为最终的并不符合我的目的.

我是一个c#开发人员,我正在尝试在androID中实现类似下面的东西

private DialogResult ShowWarningMessageBox(string errorMessage)    {        DialogResult result = MessageBox.Show(errorMessage,Resources.WarningCaption.ToString(),MessageBoxbuttons.YesNo,MessageBoxIcon.Asterisk,MessageBoxDefaultbutton.button1);        return result;    }

任何帮助将不胜感激

解决方法 完整的解决方案尝试这

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) {                    }                });
总结

以上是内存溢出为你收集整理的android – Dialogs泛型函数 – 根据用户按钮返回布尔值全部内容,希望文章能够帮你解决android – Dialogs泛型函数 – 根据用户按钮返回布尔值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存