如何将onDismissListener写入AlertDialog构建器?

时间:2016-04-28 06:58:06

标签: android

请参阅以下代码。

final CharSequence[] cs = DeviceList.toArray(new CharSequence[DeviceList.size()]);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle("Pick the Item");

    builder.setItems(cs, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            //do something
        }

    });

    builder.show(); 

请帮助我在此代码中实现构建器onDismissListener事件?

1 个答案:

答案 0 :(得分:5)

你走了:

builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

        }
    });

因此,完整的代码将类似于:

final CharSequence[] cs = DeviceList.toArray(new CharSequence[DeviceList.size()]);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setTitle("Pick the Item");

    builder.setItems(cs, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            //do something
        }

    });
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

        }
    });

    builder.show();