Goodnight! I have in the program more than 30 buttons for which onClickListener is onClickListener in one class, that is:

 `button.setOnClickListener(mylistener);` 

and a little lower:

 public OnClickListener mylistener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_mm: инструкции break; И Ρ‚Π°ΠΊ Π΄Π°Π»Π΅Π΅ } } } 

What is the essence of the question: this method OnClickListener must be transferred to a separate class. How to do it? How then to "assign" a button to it? I apologize for errors in the text, as I am writing from the tablet.

    2 answers 2

    I am writing from what I understood

      public class CustomClickListener implements View.OnClickListener { @Override public void onClick(final View v) { //Ρ‚Π²ΠΎΡ€ΡŽ, Ρ‡Ρ‚ΠΎ Ρ…ΠΎΡ‡Ρƒ } } button.setOnClickListener(new CustomClickListener()); 
    • @Andreich, it worked, thanks. But how from this class to execute a method in Main.class? - Helisia
    • I did not understand the question at all. If you want Main.class to be sent as a listener to button.setOnClickListener (), then you need to make Main.class implement the View.OnClickListener interface - andreich
    • I understood the question. We need to pass this same main as a parameter in the customclicklistenera constructor. Then in custom you create customCliclListener listener = new customOnClickListener (this), and in the listener itself to call methods you use the construct main.method (); But this is all a perversion - if you can transfer the method to the class of the listener, then it is better to do it - Uraty

    Any widget (control) in Android is inherited from the base View class. In turn, each View can be assigned an identifier (hereinafter referred to as ID ).

     <Button android:id="@+id/my_button" .... /> 

    ID are usually assigned in xml and are used to find controls in the current tree of all View.

     Button myButton = (Button) findViewById(R.id.my_button); 

    Detection of a click ( click ) on any View (not necessarily a Button ) occurs through the means of implementing the View.OnClickListener interface. This can be done in two ways: by implementing this interface Activity or creating an instance of an anonymous class ( new View.OnClickListener() ). But the main thing that needs to be done is to override the OnClick() method of the OnClick() interface. Yes, and do not forget that you need to assign the necessary control to his listener ( view.setOnClickListener(onClickListener) ). In addition, one listener can be assigned to any number of controls.

    How to distinguish which View was clicked? Just look at the signature of the onClick method of the onClick interface:

     public abstract void onClick (View v){} 

    Inside this method, developers allow us to use the link to the View that was clicked (in this case, View v ). It remains only to distinguish different views according to their individual characteristics. Here the ID comes to the rescue. The view.getId () method returns us the value of the identifier of the specific control. It remains only to match it.

      @Override public void onClick(View v) { switch (v.getId()) { case R.id.my_button1: //выполняСм Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ дСйствиС break; // вызываСтся для Π²Ρ‹Ρ…ΠΎΠ΄Π° ΠΈΠ· Π±Π»ΠΎΠΊΠ° switch (ΠΏΡ€ΠΈ Π²Ρ‹Π·ΠΎΠ²Π΅ дальнСйший ΠΊΠΎΠ΄ Π½Π΅ исполняСтся) case R.id.my_button2: //выполняСм Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ дСйствиС break; case R.id.my_button3: //выполняСм Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ дСйствиС break; case R.id.my_button4: //выполняСм Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠ΅ дСйствиС break; } } 

    ps. It should be noted that for the system ID is a value of type int.

    pps. There is 1 more way to detect depression by directly calling the method:

      <Button android:onClick="method" ... /> 

    In this case, the specified method is simply called. Read more about this option here.

    • beautiful answer to another question. asked not that ... - Yura Ivanov