确定哪个AlertDialog触发onClick(DialogInterface对话框,int哪个)

Mon*_*eur 8 events android dialog onclick identify

我正在创建一个如下对话框:

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DIALOG_1:
   return new AlertDialog.Builder(this)
   .setTitle(R.string.s_dlg1)
   .setPositiveButton(android.R.string.ok, this)
   .create();

  case DIALOG_2:
   ...
   ...
  }

  return null;
 }

 @Override
 public void onClick(DialogInterface dialog, int whichButton) {
  if (dialog == ???) {
   ...
  }
  else if (dialog == ???){
   ...
  }
 }
Run Code Online (Sandbox Code Playgroud)

如何识别触发onClick方法的对话框?在创建对话框时,我无法将接口方法声明为内联,因为我想在我的类中访问变量.每个其他接口都将某种id传递给它的方法,以识别哪个对象称为方法,但我似乎无法对"DialogInterface对话框"做任何事情.

Han*_*ans 1

也许您可以将 onclick 侦听器提取为单独的类,然后传入对话框 id?该接口是 android.content.DialogInterface.OnClickListener