Just create a named function instead of an anonymous one:
View.OnLongClickListener listener = new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Button clickedButton = (Button) v;
String buttonText = clickedButton.getText().toString();
Log.v(TAG, "button long pressed --> " + buttonText);
return true;
}
};
button1.setOnLongClickListener(listener);
button2.setOnLongClickListener(listener);
button3.setOnLongClickListener(listener);
And, altough you don't asked it, we must warn you: As @AndyRes says, your buttons must have differents ids. Having the same ID doesn't mean that getting the button by id will return all buttons, only will return the first button with that ID, that's because it only works with the first button.