从 Java 中的匿名类引用变量
我正在编写一个 setonclick 监听器,我希望能够引用该按钮,以便我可以更改其属性。即使其禁用?
我收到此消息:
无法引用在不同方法中定义的内部类中的非最终变量confirmButton
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
confirmButton.setEnabled(false);
}
});
I am writing a setonclick listner, and I want to be able to refer to that button so that I can change its properties. I.e. make it disabled?
I get thismessage:
Cannot refer to a non-final variable confirmButton inside an inner class defined in a different method
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
confirmButton.setEnabled(false);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为您可能试图从以这种方式使用的匿名类访问该按钮:
这不起作用,因为在 Java 中,匿名类无法看到在声明它们的方法中声明的变量,因为它们的作用域是分开的。让您的类看到它的唯一方法是强制使用
final
约束,该约束可确保编译器变量在初始化后不会更改,从而允许其将其范围扩展到匿名类。要快速解决此问题,您可以从
actionPerformed
内的ActionEvent
访问按钮:否则,您必须在某处具体声明您的
ActionListener
或声明按钮在具有static
或final
属性的方法之外。特别是如果您计划通过另一个元素触发的操作来修改某些元素。This because you are probably trying to access that button from an anonymous class that you use in this way:
This doesn't work because in Java anonymous classes cannot see variables declared in methods in which they are declared too since their scope are separated. The only way to let your class see it is forcing the
final
constraint which assures the compiler that the variable won't change after being initialized, allowing it to extend its scope to the anonymous classes.To quickly fix this you can access the button from the
ActionEvent
inside theactionPerformed
:Otherwise you have to concretely declare your
ActionListener
somewhere or declare the buttons outside the method withstatic
orfinal
attribute.. especially if you plan to modify some elements by an action that is fired by another element.我建议不要使用
getSource
;该文档并不保证它将成为您的按钮。您可以使按钮在作用域中成为final
,或者使用更复杂的类。如果您转向操作和侦听器类的设计而不是匿名子类,那么您将获得更多重用的机会(您已经可以看到 ComponentRelevantOnClickListener 可以替换为“DisableOnClickListneer”,您可以在任何地方使用它来实现此目的),并且您的代码总体上将得到更好的设计。
I would recommend against the
getSource
; the documentation doesn't promise that it will be your button. You can either make your buttonfinal
in the scope, or use a more sophisticated classIf you move toward a design of action and listener classes instead of anonymous subclasses, you get more chance for re-use (you can already see that
ComponentRelevantOnClickListener
could be replaced with a "DisableOnClickListneer" that you can use anywhere for this purpose), and your code will be overall better designed.在 Java 中,匿名类中引用的变量需要定义为
final
。 Jon Skeet 在这篇文章中提供了一个很好的例子。vars that are referenced within anonymous classes need to be defined as
final
in Java. Jon Skeet has a great example of this nestled within this article.匿名内部类只能访问外部作用域中的变量(如果它们是
final
)。假设您只分配给confirmButton
一次,我建议只需将其标记为final
。Anonymous inner classes can only access variables from the outer scope if they are
final
. Assuming you only assign to theconfirmButton
once, I suggest simply tagging it asfinal
.