关闭自定义alertdialog onclicklistener

时间:2013-02-23 07:50:59

标签: java android android-alertdialog onclicklistener

我有三个按钮的自定义对话框。 我有3个按钮的点击监听器.... 这是代码。

 public void addDialog() {
        // TODO Auto-generated method stub
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            // Setting Dialog Title
            alertDialog.setTitle("Add From");
            // Setting Dialog Message
            alertDialog.setMessage("Add Number: ");
            LayoutInflater layoutInflater 
            = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view=layoutInflater.inflate(R.layout.dialog_lay,null);

           Button btn_Contact = (Button)view.findViewById(R.id.btn_contact);
           Button btn_SMS = (Button)view.findViewById(R.id.btn_sms);
           Button btn_Manually = (Button)view.findViewById(R.id.btn_manually);
           //  Setting Negative "NO" Button
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                // Write your code here to invoke NO event
                Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                dialog.cancel();
                }
            });

            OnClickListener listenerDial = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
            // i want to close alert dialog here    

            Toast.makeText(getApplicationContext(), "You clicked contact btn", Toast.LENGTH_SHORT).show();

                }
            };

            // add listener to button.
            btn_Contact.setOnClickListener(listenerDial);
            btn_SMS.setOnClickListener(listenerDial);
            btn_Manually.setOnClickListener(listenerDial);
            alertDialog.setView(view);
            alertDialog.show();

    }

我想在按下三个键中的任何一个时关闭此警告对话框。 任何建议pzzzz ..

4 个答案:

答案 0 :(得分:0)

使用alertDialog.dismiss()关闭它。

答案 1 :(得分:0)

OnClickListener listenerDial = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
        // i want to close alert dialog here    

        alertDialog.dismiss();
        Toast.makeText(getApplicationContext(), "You clicked contact btn",        
        Toast.LENGTH_SHORT).show();

            }
        };

答案 2 :(得分:0)

我修改你的代码只是检查出来

 public void addDialog() {
    // TODO Auto-generated method stub
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        // Setting Dialog Title
        alertDialog.setTitle("Add From");
        // Setting Dialog Message
        alertDialog.setMessage("Add Number: ");
        LayoutInflater layoutInflater 
        = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.dialog_lay,null);

       Button btn_Contact = (Button)view.findViewById(R.id.btn_contact);
       Button btn_SMS = (Button)findViewById(R.id.btn_sms);
       Button btn_Manually = (Button)findViewById(R.id.btn_manually);
       //  Setting Negative "NO" Button
        alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
            // dialog.dimiss(); // dialog will dismiss when you click on this button un-comment it so it works.
            }
        });

        OnClickListener listenerDial = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            dialog.dimiss(); // dialog will dismiss when you click on this button

        Toast.makeText(getApplicationContext(), "You clicked contact btn", Toast.LENGTH_SHORT).show();

            }
        };

        // add listener to button.
        btn_Contact.setOnClickListener(listenerDial);
        btn_SMS.setOnClickListener(listenerDial);
        btn_Manually.setOnClickListener(listenerDial);
        alertDialog.setView(view);
        alertDialog.show();

}

答案 3 :(得分:0)

刚添加

final AlertDialog Dial = alertDialog.create(); 

并更改

dialog.setView(layout); to Dial.setView(layout);

现在只需致电 Dial.dismiss(); in onclick listener ..对我来说很好。