0
mButton.setOnClickListener(new View.OnClickListener(){  
    @Override  
    public void onClick(View v){  
        //Temp Empty  
    }  
});  

Now my main cause of confusion is this View.OnClickListener(){}. From the API, I'm understanding that this is an interface. However, I haven't really seen interfaces declared in such a way before.

Here are my questions:
1. "View.OnClickListener" is an interface correct? OnClickListener extends View?
2. Why are there parentheses after "OnClickListener"? Parameters?
3. Why are there brackets after the parentheses? Overriding the initial onClick method in view?

I'm sorry for asking so much about a little piece code, but thanks for your help!

user2946632
  • 111
  • 1
  • 9

3 Answers3

0

These are called anonymous functions.

This

mButton.setOnClickListener(new View.OnClickListener(){  
    @Override  
    public void onClick(View v){  
        //Temp Empty  
    }  
});  

Is same as

View.OnClickListener listner = new OnClickListener () {

                                 @Override
                                 public void onClick (View v) {

                                     // TODO Auto-generated method stub

                                 }
                             };
mButton.setOnClickListener(listner);  
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
0

It's an anonymous class. You can read more about it @ http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html or search google for java anonymous classes for a few more tutorials, explanations.

gentwo
  • 113
  • 9
0

Basically, within the View class (in Android) there exists the interface OnClickListener like so:

public interface OnClickListener{
    public void onClick(View v);
}

These interfaces act as place holders for functions that you (the developer) will define later on. Somewhere within the Android View class, there is functionality written for when a user's pointer (finger) touches the a view. Within this code, the SDK calls the method onClick(v), which acts as a placeholder for code that will be defined later. When you (the developer) implement this method by adding a new OnClickListener, you essentially inject your code into that placeholder within the View class that calls onClick(v);

As far as the @Override annotation, I will point you to this answer which explains it well.

Community
  • 1
  • 1
ChrisMcJava
  • 2,145
  • 5
  • 25
  • 33