Why pass an interface as an argument to a function in android? -


while learning event handling in android came across below code

here few questions have

1) why instance of anonymous class implementing view.onclicklistener() interface passed argument setonclicklistener()?

2) benefit of passing instance argument?

 button button    =    (button) findviewbyid(r.id.button_send);  button.setonclicklistener(new view.onclicklistener() // explain {  public void onclick(view v) {     // in response button click }}); 

the goal: want when user clicks on button.

what need: need know when user clicks on button.

how know: using view.onclicklistener interface.

this source code of view.onclicklistener:

    /**      * interface definition callback invoked when view clicked.      */     public interface onclicklistener {         /**          * called when view has been clicked.          *          * @param v view clicked.          */         void onclick(view v);     } 

this pass method method b, , method b invokes method when event happen.

in view.onclicklistener case, method pass invoked when user clicks on button.

technically, implement view.onclicklistener , create instance of on fly , pass method setonclicklistener().

the onclick(view v) method invoked when user taps on button. code inside onclick(view v) method executed whenever user taps on button.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -