8

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

 @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 == ???){
   ...
  }
 }

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

4

6 回答 6

1

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

于 2010-02-22T06:10:11.640 回答
0

这对我有用

 case Dialog_import_database:
            return new AlertDialog.Builder(Main.this)
            .setTitle(R.string.ImportDatabaseDialogTitle)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Log.i("Main","positive Button In dialog Box");

当点击这里的肯定按钮时,你想要什么

                }
            })
            .setNegativeButton("Cancel", null)
            .setMessage(R.string.ImportDatabaseMessage)
            .create();
        }
于 2012-01-15T11:30:32.633 回答
0

这是一个老问题,但我仍然遇到同样的问题 xO。作为一种解决方法,我使用了 Dialog 对象的“hashCode”。根据对话框类型,您可以将“对话框接口”转换为您的特定类型,并将“第一个”和“第二个”对话框的“hashCode”与回调函数中的“DilaogInterface”对象的“hasCode”进行比较。

“公共无效 onClick(DialogInterface 对话框,int whichButton){}”

函数签名。

于 2020-02-25T17:18:28.747 回答
0

这个问题已经很老了,到目前为止还没有答案,所以我找到了解决这个我也遇到的确切问题的解决方案。

在我启动的这个活动中,根据用户的操作,有可能触发 2 个不同的 custom DialogFragment。让我们称他们为DialogFragment AB。我们使用标准构建器创建这些标签,然后.show(fragmentManager, <TAG>)标签可以在哪里A_TAGB_TAG以后轻松识别它们。

因此,显示了其中任何一个DialogFragment,您将需要决定在您的实施中做什么,onClick(DialogInterface dialog, int buttonClicked)这就是我这样做的方式:

final DialogFragment aDialogInterface = (DialogFragment) getSupportFragmentManager().findFragmentByTag(A_TAG);
final DialogFragment bDialogInterface = (DialogFragment) getSupportFragmentManager().findFragmentByTag(B_TAG);

if (aDialogInterface != null) {
            //do something for dialogfragment A
} else if (bDialogInterface != null) {
            //do something for dialogfragment B
}

findFragmentByTag(...)基本上对...进行空检查

于 2018-05-04T09:14:16.480 回答
0

我遇到了同样的问题。现在我找到了以下解决方案。

private Dialog[] aaa = new Dialog[2];

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

  case DIALOG_2:
   ...
   ...
  }

  return null;
 }

 @Override
 public void onClick(DialogInterface dialog, int whichButton) {
  if (dialog == aaa[0])) {
   ...
  }
  else if (dialog == aaa[1]){
   ...
  }
 }
于 2019-01-11T09:40:03.773 回答
-1

很难直接识别显示了哪个对话框而不是按下了哪个按钮,因此如果您使用不同的按钮填充对话框,您就可以做到这一点。像这样的东西:

new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg1)
.setPositiveButton(android.R.string.ok, this)
.create();

new AlertDialog.Builder(this)
.setTitle(R.string.s_dlg2)
.setNegativeButton(android.R.string.ok, this)
.create();

@Override
public void onClick(DialogInterface dialog, int whichButton) {
if (whichbutton == DialogInterface.BUTTON_POSITIVE) {
...
 }
else if (whichButton == DialogInterface.BUTTON_NEGATIVE){
 ...
 }
于 2015-06-05T13:03:10.120 回答