Android AlertDialog Builder更好的代码

时间:2016-05-06 22:27:48

标签: android groovy

我得到以下代码,在Android中生成一个Alert对话框:

new AlertDialog.Builder(actContainer).
            setIcon(R.mipmap.ic_attach_file_black_24dp).
            setTitle(strTitle).
            setMessage(Html.fromHtml(strHtml)).
            setCancelable(false).
            setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Log.i("OptimGeek", "prrrrrrr . . .")
                        }
                    }
            ).
            show()

这是在groovy,实际上它工作得很好,如何改善“setPossitiveButton”以避免内部类?

提前致谢

1 个答案:

答案 0 :(得分:1)

使用SAM类型强制或Implicit Closure Coercion

new AlertDialog.Builder(actContainer).
    setIcon(R.mipmap.ic_attach_file_black_24dp).
    setTitle(strTitle).
    setMessage(Html.fromHtml(strHtml)).
    setCancelable(false).
    setPositiveButton("OK", { DialogInterface dialogInterface, int i ->
        Log.i("OptimGeek", "prrrrrrr . . .")
    } as DialogInterface.OnClickListener).
    show()