How can i pass parameter to an OnClickListener() ?
Got my Listener:
OnClickListener myListener = new OnClickListener() { @Override public void onClick(View v) { //I want to reach params here } };
I got 12 buttons and i dont want to write 12 listeners for them, it would be great to just pass a string to them and they can do completly different things.
It's very easy to Pass parameter to JavaScript function using the onClick() function. If sending a string value then use double” ” or single ” quote in the function.
To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .
Use the onclick attribute in a button tag with the function name and pass value in this function. With this method, you can also take input from users and pass parameters in the JavaScript function from HTML.
Use your own custom OnClickListener
public class MyLovelyOnClickListener implements OnClickListener { int myLovelyVariable; public MyLovelyOnClickListener(int myLovelyVariable) { this.myLovelyVariable = myLovelyVariable; } @Override public void onClick(View v) { //read your lovely variable } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With