java 在android中实现关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5769036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:41:25  来源:igfitidea点击:

Implements keyword in android

javaandroid

提问by sab

In Android I am seeing code like this:

在 Android 中,我看到这样的代码:

public class Now extends Activity implements View.OnClickListener

Is this legal in java? What exactly is View.OnClickListener representing here.

这在java中合法吗?View.OnClickListener 在这里代表什么。

回答by Nanne

The question seems to be about the use of View.x. As @Vicente_Plata nicely put it in the comments:

问题似乎是关于View.x. 正如@Vicente_Plata 在评论中很好地指出的那样:

OnClickListener is an interface declared inside the View class. Like an "inner class" but, rather, an "inner interface

OnClickListener 是在 View 类中声明的接口。就像一个“内部类”,而是一个“内部接口”



旧答案:

View.OnclickListener 是一个 interface界面。它定义了您的活动必须实现的方法,在本例中为OnClickListener()OnClickListener(). 任何检查的人都想知道您的班级中是否有这样的功能,因此他们可以将其NowNow用作OnclickListenerOnclickListener.

An interface does not provide an implementation.

接口不提供实现。

Also, from this page

另外,从这个页面

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

实现一个接口允许一个类在它承诺提供的行为方面变得更加正式。接口构成了类和外部世界之间的契约,并且这个契约在构建时由编译器强制执行。如果您的类声称实现了一个接口,则该接口定义的所有方法都必须出现在其源代码中,然后该类才能成功编译。

回答by Aleadam

OnClickListeneris the interface used to receive click events. You need to override the onClick()method and implement your own code to deal with it. http://developer.android.com/reference/android/view/View.OnClickListener.html

OnClickListener是用来接收点击事件的接口。您需要覆盖该onClick()方法并实现您自己的代码来处理它。http://developer.android.com/reference/android/view/View.OnClickListener.html

You can take a look at the Viewclass structure here: http://developer.android.com/reference/android/view/View.html#nestedclasses

您可以在View此处查看类结构:http: //developer.android.com/reference/android/view/View.html#nestedclasses

And browse the implementation here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View.java

并在此处浏览实现:http: //grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View.java

You may want to read the android Dev Guide (specially the topic to handle UI events): http://developer.android.com/guide/topics/ui/ui-events.html

您可能需要阅读 android 开发指南(特别是处理 UI 事件的主题):http: //developer.android.com/guide/topics/ui/ui-events.html

The following two are equivalent:

下面两个是等价的:

import android.view.View;
public class Now extends ActivityGroup implements OnClickListener {

import android.view.*;
public class Now extends ActivityGroup implements View.OnClickListener {


The following are not needed after the clarification

澄清后不需要以下内容

And perhaps the Oracle's java interface tutorial:

或许还有 Oracle 的 java 接口教程:

http://download.oracle.com/javase/tutorial/java/concepts/interface.html

http://download.oracle.com/javase/tutorial/java/concepts/interface.html

http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html

http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html

回答by Ted Hopp

As others already pointed out, implements View.OnClickListenerspecifies that your class implements the methods defined in the View.OnClickListenerinterface. If the dot notation makes you uncomfortable, you can import the interface explicitly:

正如其他人已经指出的那样,implements View.OnClickListener指定您的类实现View.OnClickListener接口中定义的方法。如果点符号让你不舒服,你可以显式导入接口:

import android.view.View.OnClickListener;

public class Now extends Activity implements OnClickListener
// ...