How to call a method in the View using a Class that extends Fragment

0

I have several buttons in a view that call the method addNumero in OnClick, something like this:

<Button
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:gravity="center"
  android:onClick="addNumero"
  android:text="8" />

And since there are many buttons, it is impractical to use setOnClickListener in each. I have a class that inherits from fragment: public class Exemplo extends Fragment and this class inflates the view that contains the buttons and in this class is also the addNumero method but it is not working, when I click do not call the addNumber.

    
asked by anonymous 19.01.2018 / 13:09

1 answer

1

The onClick attribute only works when the view is being used in an Activity, you will have to implement a View.OnClickListener .

If you have many on-screen buttons with similar functions, you can create a single class to use as Listener.

private class MyClickListener implements View.OnClickListener

If the buttons have different functions, create several classes in separate files, so even with a lot of code, everything will be organized.

Sources:

link

link

EDIT: Enlightened answer with the help of link

    
19.01.2018 / 14:28