2013-02-23 82 views
-1

我有三個按鈕的自定義對話框。 我有點擊監聽3個按鈕.... 這裏是代碼。關閉clicklistener上的自定義alertdialog

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

    } 

我要的就是按下任何三個關鍵的時候關閉該警告對話框.. 任何suggetion plzzzz ..

回答

0

新添加

final AlertDialog Dial = alertDialog.create(); 

和改變

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

現在只需撥打Dial.dismiss(); in onclick listener ..對我來說工作得很好。

0

使用alertDialog.dismiss()將其關閉。

+0

它崩潰我的應用程序.. – 2013-02-23 09:45:57

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

      } 
     }; 
+0

我已檢查您的答案,但它在點擊崩潰。 – 2013-02-23 09:45:04

+0

首先告訴我你想要什麼,或者在你的代碼中嘗試什麼????? – duggu 2013-02-23 09:48:44

+0

看到這個 duggu 2013-02-23 10:01:08

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

} 
+0

對話框參考沒有定義。 – 2013-02-23 09:45:41

+0

在你的按鈕findViewById你必須這樣做 alertDialog.findViewById這將引用按鈕到對話框。嘗試一下,如果它不起作用,你可以發佈logcat! – k0sh 2013-02-23 11:01:35