android-如何关闭AlertDialog.Builder对话框

android-如何关闭AlertDialog.Builder对话框

前言
在实际写代码的时候,AlertDialog.Builder的使用算是比较频繁的,在对AlertDialog.Builder关闭的时候,我们会发现,AlertDialog.Builder对话框没有相对应的finish()或者dismiss()这样的方法可使用。

那么我们在关闭的时候怎么才能做到呢?

##AlertDialog
父类AlertDialog有dismiss方法,可以实现对话框的关闭,而且AlertDialog.Builder在.show()的时候会得到一个AlertDialog对象,用dismiss方法将该Builder关闭。

##自定义的AlertDialog.Builder布局示例
这里写图片描述

/**
     * 设置dialog的出现
     * @param hangTag
     */
    private void showCustomizeDialog(HangTag hangTag) {
        final AlertDialog.Builder customizeDialog = new AlertDialog.Builder(getActivity());
        customizeDialog.setCancelable(false);
        View infoview = LayoutInflater.from(getActivity()).inflate(R.layout.wr_dialog_hangtag_info,
                null, false);
        TextView exchangeName, volume, marketPirce, region, irrigation, administrative, watertype,exchangeTime;
        Button backColse;
        exchangeTime = (TextView) infoview.findViewById(R.id.exchange_time);
        exchangeName = (TextView) infoview.findViewById(R.id.exchangeName);
        volume = (TextView) infoview.findViewById(R.id.volume);
        marketPirce = (TextView) infoview.findViewById(R.id.marketPirce);
        region = (TextView) infoview.findViewById(R.id.region);
        irrigation = (TextView) infoview.findViewById(R.id.irrigation);
        administrative = (TextView) infoview.findViewById(R.id.administrative);
        watertype = (TextView) infoview.findViewById(R.id.watertype);
        backColse = (Button) infoview.findViewById(R.id.back_colse);
        exchangeTime.setText(hangTag.getGprq()+"");
        exchangeName.setText(hangTag.getName());
        volume.setText(hangTag.getSl() + "");
        marketPirce.setText(hangTag.getJg() + "");
        region.setText(hangTag.getSzdq());
        irrigation.setText(hangTag.getSzgq());
        administrative.setText(hangTag.getSzglq());
        if (hangTag.getWaterType() == 8101) {
            watertype.setText("地下水");
        } else if (hangTag.getWaterType() == 8100) {
            watertype.setText("地表水");
        }
        customizeDialog.setView(infoview);
        final AlertDialog dialog = customizeDialog.show();
        /**
         * 返回
         */
        backColse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

以上就是全部内容,仅做笔记,不喜勿喷!

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值