Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中如何调用匿名内部类_Java_Android - Fatal编程技术网

java中如何调用匿名内部类

java中如何调用匿名内部类,java,android,Java,Android,代码中有一个警报框(用于注销功能)。 此警报框在一个方法(即注销方法)内创建,然后向其中匿名添加两个onClickListener。 我怎样才能从外部呼叫这些匿名侦听器 代码: 我需要的是以某种方式调用这个onClick方法并传递同一对话框的实例。 我已经阅读过使用反射进行此操作的示例,但在这些示例中,匿名类是一个子类,即捕捉到“new”的返回值如果没有该对象的任何引用,则不能调用它。 您可以执行以下操作: AlertDialog.Builder builder DialogInterface.

代码中有一个警报框(用于注销功能)。 此警报框在一个方法(即注销方法)内创建,然后向其中匿名添加两个onClickListener。 我怎样才能从外部呼叫这些匿名侦听器

代码:

我需要的是以某种方式调用这个onClick方法并传递同一对话框的实例。
我已经阅读过使用反射进行此操作的示例,但在这些示例中,匿名类是一个子类,即捕捉到“new”的返回值

如果没有该对象的任何引用,则不能调用它。
您可以执行以下操作:

AlertDialog.Builder builder
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener(){
   public void onClick(DialogInterface dialog, int id) {
      //some logic
   }
builder.setPositiveButton("Yes", listener);
}
// now you can call function on if like
listener.SomeFunction()

有关更多详细信息,请参阅。

您可以将侦听器设置为字段变量

private final DialogInterface.OnClickListener dialogYesListener = new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
      //some logic
   }
};

AlertDialog.Builder builder
builder.setPositiveButton("Yes", dialogYesListener);
您有两个选择:

1) 重构代码,使其引用到
DialogInterfact.OnClickListener
的实例,如下所示:

AlertDialog.Builder builder;
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
         //some logic
    }
}


builder.setPositiveButton("Yes", listener);
DialogInterface.OnClickListener listener =
   builder.getPositiveButton().getListener(); //adjust this to a real API
2) 我不知道是否有这样的API,但如果有,您可以尝试从构建器中提取侦听器实现伪代码应如下所示:

AlertDialog.Builder builder;
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
         //some logic
    }
}


builder.setPositiveButton("Yes", listener);
DialogInterface.OnClickListener listener =
   builder.getPositiveButton().getListener(); //adjust this to a real API

从外部调用它是什么意思?您可以创建一个引用变量作为全局变量,然后在内部类中使用时,使用
this
关键字用匿名类初始化它。或者您可以创建一个单独的方法,并使用该类的引用作为该方法的参数。您不能,它没有名称。这就是为什么它叫匿名。如果你是这个意思的话,无论如何。