2010-01-28 41 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 
    } 
}); 

,做你的按钮回调需要。