如何从模态对话框中获得响应?

Jos*_*ega 2 java android

我目前有这个:

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

如何通过设置一个事件监听器来捕获用户单击是或否?

Dav*_*ebb 6

当你打电话setPositiveButton()setNegativeButton()不是传球时null你应该传球DialogInterface.OnClickListener.

例如:

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