закрыть настраиваемый диалог предупреждений на clicklistener

у меня есть пользовательский диалог с тремя кнопками. у меня есть прослушиватель кликов для 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();

    }

Я хочу закрыть это диалоговое окно с предупреждением при нажатии любой из трех клавиш.. любое предложение, пожалуйста..


person Abdus Salam    schedule 23.02.2013    source источник


Ответы (4)


Используйте alertDialog.dismiss(), чтобы закрыть его.

person Rahil2952    schedule 23.02.2013

я изменил ваш код, просто проверьте его

 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();

}
person Kosh    schedule 23.02.2013
comment
ссылка на диалог не определена. - person Abdus Salam; 23.02.2013
comment
в вашей кнопке findViewById вы должны сделать это так, как это alertDialog.findViewById это будет ссылаться на кнопки в диалоговом окне. попробуйте, и если это не сработает, вы можете опубликовать logcat! - person Kosh; 23.02.2013

Только что добавлен

final AlertDialog Dial = alertDialog.create(); 

и изменить

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

теперь просто позвоните Dial.dismiss(); in onclick listener.. у меня работает нормально.

person Abdus Salam    schedule 25.02.2013

person    schedule
comment
Я проверил ваш ответ, но он вылетает при нажатии. - person Abdus Salam; 23.02.2013
comment
см. этот ‹stackoverflow .com/questions/5713312/ - person duggu; 23.02.2013
comment
вы также можете попробовать MainActivity.this.alertDialog.dismiss(); - person duggu; 23.02.2013
comment
я хочу нажать на кнопку, и она откроет другое действие. и закрыть диалоговое окно, но диалоговое окно не закрывается. - person Abdus Salam; 25.02.2013