Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View.OnClickListener, method or class?

Tags:

java

android

sorry if this question is stupid, but I can't wrap my head around Java syntax..I learnt C/C++
I know View is a class which is good..but I don't understand if View.OnClickListener() is a method.
I doubt it unless it returns an object?
I think View is a class which has a static OnClickListener member object..again that doesn't make sense to me..
Can some explain what is happening with this line of code?

button1 = (Button) findByView(R.id.button1)  ;
    button1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

        }
    }

So what is happening with this code?

Button1 is a reference to the button1 object in the xml file.

button1 object has a member object setOnClickListener which I from its name I assume initializes an event to the button or something. But it receives View.OnClicListener() object.

I am confused by that..onClick receives a View object so onClickListener is not an object returns a View object?? I don't get it at all.

Can someone explain what happens in that line View.onClickListener() is it another way of saying new this?

like image 730
Lews Therin Avatar asked Aug 04 '11 18:08

Lews Therin


People also ask

What is view OnClickListener ()?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

Which class contains onClick () method?

To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.

What is the difference between onClick and OnClickListener?

OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.

What is OnClickListener in Kotlin?

Kotlin Android – Set OnClickListener for Button Android Button widget is a UI element generally used to receive user actions as input. You can click on a Button, long press, etc. In this tutorial, we shall learn to set OnClickListener for Button.


2 Answers

View.OnClickListener is an interface, you don't call it, but creates a new instance of it (new View.OnClickListener() is a call to the constructor)

The instance you create is of anonymous class that implements View.OnClickListener, in the brackets right under new View.OnClickListener()

Any class that implements View.OnClickListener must implement the methods declared in it (e.g. onClick)

setOnClickListener just saves the reference to the View.OnClickListener instance you supplied, and when someone clicks the button, the onClick method of the listener you set is getting called.

like image 178
MByD Avatar answered Oct 17 '22 07:10

MByD


OnClickListener is an interface. An interface provides a set of Methods other classes can implement. http://download.oracle.com/javase/tutorial/java/concepts/interface.html

You could have another class (Like and adapter), that extends OnClickListener, then your Adapter class could add the method "OnClick(View v)", and it would also be able to handle Click events. Or you could use the code you posted, where you just create an anonymous class, that implements OnClickListener.

-Woody

like image 2
Woody Avatar answered Oct 17 '22 08:10

Woody