Android View Parameter for OnClickListener Method

Please do not mind if a stupid question, but please, I need to clear my confusion.

For OnClickListeneron the button in android I put this inmain.xml

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="Button"
    android:onClick="clicked"
/>

and in java file I did

  public void clicked(View v)
  {
   //  my code here 
  }

my question is why do we need to pass View viewwhen we call the method clicked.

+5
source share
2 answers

AFAIK,

Because the method is called without getting a button in onCreate. And to access this button you need to have an idea.

those. View v is a representation of your button.

, , . findViewbyId?      

Button b=(Button)v;

String text=b.getText();

, findViewbyId

, , . . findViewbyId.

+9

, v.getId(), , .

, , , , .

+2

All Articles