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布局示例
android-如何关闭AlertDialog.Builder对话框_第1张图片

/**
     * 设置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();
            }
        });
    }

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

更多文章资源,欢迎关注:程序猿阵线联盟

android-如何关闭AlertDialog.Builder对话框_第2张图片

你可能感兴趣的:(android-如何关闭AlertDialog.Builder对话框)