2010-01-28 39 views
2

我現在有這樣的:如何從模態對話框獲得響應?

Builder yesandno = new AlertDialog.Builder(this);   
yesandno.setTitle("QuickResponse"); 
yesandno.setMessage(message); 
yesandno.setPositiveButton("YES", null); 
yesandno.setNegativeButton("NO", null); 
yesandno.show(); 

我應該如何去設置一個事件監聽器,將捕獲如果用戶點擊YES或NO?

回答

6

當您撥打setPositiveButton()setNegativeButton()而不是通過null您應通過DialogInterface.OnClickListener

例如:

yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     //User clicked yes! 
    } 
}); 
4

就做這樣的事情:

yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     // User clicked yes 
    } 
}); 
yesandno.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int id) { 
     // User clicked no 
    } 
}); 

,做你的按鈕回調需要。