代码之家  ›  专栏  ›  技术社区  ›  YaW

关闭自定义对话框?

  •  9
  • YaW  · 技术社区  · 14 年前

    我正在尝试创建一个自定义对话框来显示此对话框中的视图。这是生成器代码:

    //Getting the layout
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_dialog_simple,
                                   (ViewGroup) findViewById(R.id.rlDialogSimple));
    
    //Change Text and on click
    TextView tvDialogSimple = (TextView) layout.findViewById(R.id.tvDialogSimple);
    tvDialogSimple.setText(R.string.avisoComprobar);
    Button btDialogSimple = (Button) layout.findViewById(R.id.btDialogSimple);
    btDialogSimple.setOnClickListener(new OnClickListener() {
    
        public void onClick(View v) {
            //Do some stuff
    
            //Here i want to close the dialog
        }
    });
    
    AlertDialog.Builder builder = new AlertDialog.Builder(AcPanelEditor.this);
    builder.setView(layout);
    AlertDialog alert = builder.create();
    alert.show();
    

    所以,我想取消btDialogSimple的onClick中的对话框。我该怎么做?我不知道如何从onclicklistener内部调用dismish方法。

    我的按钮有一个自定义的布局,所以我不想做一个builder.setPositiveButton。

    有什么想法吗?

    4 回复  |  直到 14 年前
        1
  •  17
  •   Pentium10    14 年前

    必须将AlertDialog保存到父类属性中,然后使用以下方法:

    class parentClass ........ {
    private AlertDialog alert=null;
    ........
    public void onClick(View v) {
            //Do some stuff
    
            //Here i want to close the dialog
            if (parentClass.this.alert!=null)            
            parentClass.this.alert.dismiss();
        }
    ........
    this.alert = builder.create();
    this.alert.show();
    

    }

        2
  •  7
  •   source.rar    13 年前

    我想最好的办法是打电话

    dismissDialog(DIALOG_ID);
    

    将AlertDialog设为类属性是否会破坏从onCreateDialog()返回对话框的目的?

        3
  •  0
  •   Salih Kiraz    11 年前

    ± 我正在使用

    public  Dialog dialog;
    
    buton_sil.setOnClickListener(new View.OnClickListener() {
    
                @ Override
                public void onClick(View v) {
    
                       DialogClose();
                       }
    }):
    
    
    public void DialogClose() {
            dialog.dismiss();
        }
    
        4
  •  0
  •   Sazzad Hissain Khan    10 年前

    AlertDialog mAlertDialog 作为全局变量。打电话给我 mAlertDialog.dismiss(); onClick()