Could someone break this down for me?

0 Stephen Austin · October 11, 2015
I am having a hard time understanding what exactly is going on in this block of code Bucky writes when he is discussing Listeners and event handling. Here is the code:
buckysButton.setOnLongClickListener(
new Button.OnLongClickListener(){
public boolean on LongClick(View v){
TextView buckysText=(TextView)findViewById(R.id.buckysText);
buckysText.setText("Holy carp, that was a long one!");
return true;
}
}
);

and how does this compare?

protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
    }
    ...
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Gary Whitney · October 11, 2015
The top example is an anonymous listener implemented, and the bottom one is just  a template for the method.

The whole topic of callbacks is not something I won't thoroughly explain here.

The essence of the concept is:
* A clickable object can generate a event.
* Another object registers with the clickable object that it will implement the required onClick() method.
* When the clickable object is clicked, an event object is created and passed to all registered objects.

The callback concept is an essential part of GUI programming. You will have an extremely hard time understanding ANY android programming if you have not learned the Java language first.
  • 1

Java / Android Development

107,044 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator