Implements Keyword In Android
Solution 1:
The question seems to be about the use of View.x
. As @Vicente_Plata nicely put it in the comments:
OnClickListener is an interface declared inside the View class. Like an "inner class" but, rather, an "inner interface
Old answer:
View.OnclickListener is an interface. It defines methods that your activity must implement, in this case an
OnClickListener()
. Anyone checking wants to know if there is such a function in your class, so they can use Now
as an OnclickListener
.
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.
Solution 2:
OnClickListener
is 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
You can take a look at the View
class structure here: 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
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
The following two are equivalent:
import android.view.View;
publicclassNowextendsActivityGroupimplementsOnClickListener {
import android.view.*;
publicclassNowextendsActivityGroupimplementsView.OnClickListener {
The following are not needed after the clarification
And perhaps the Oracle's java interface tutorial:
http://download.oracle.com/javase/tutorial/java/concepts/interface.html
http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html
Solution 3:
As others already pointed out, implements View.OnClickListener
specifies that your class implements the methods defined in the View.OnClickListener
interface. If the dot notation makes you uncomfortable, you can import the interface explicitly:
import android.view.View.OnClickListener;
publicclassNowextendsActivityimplementsOnClickListener// ...
Post a Comment for "Implements Keyword In Android"