问题
In my Android application, I want to create a fragment that works like a keypad. I have one function which handle onClick for all 9 keys. I want to know is there anyway to write just one function to handle onLongClick for all these 9 keys too.
here is layout xml :
<Button
android:id="@id/testButton"
android:layout_width="70dp"
android:layout_height="55dp"
android:layout_margin="2dp"
android:background="@drawable/keypad_round_button"
android:text="1"
android:textColor="@color/black_1"
android:onClick="keypadSetNote"
android:longClickable="true"/>
<Button
android:id="@id/testButton"
android:layout_width="70dp"
android:layout_height="55dp"
android:layout_margin="2dp"
android:background="@drawable/keypad_round_button"
android:text="2"
android:longClickable="true"
android:textColor="@color/black_1"
android:onClick="keypadSetNote"
/>
Here is OnlongClick listener :
Button button = (Button) findViewById(R.id.testButton);
button.setOnLongClickListener(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;
}
});
I gave all keys same ID to handle all onLongClick actions in one function, but it just work for the first key. Is there anyway to define something like group button in Android ??? Or I have to write OnLongClick listener for all of them separately ???
回答1:
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.
回答2:
Buttons should have different ids:
<Button
android:id="@id/testButton1"
//... />
<Button
android:id="@id/testButton2"
//... />
<Button
android:id="@id/testButton3"
//... />
Now, to set the listener for all, a solution would be to get the id
of all buttons in an array, and then use a "for" loop to set the listener for each.
Like this:
// Store the id of buttons in an array
int[] ids={R.id.testButton1, R.id.testButton2,R.id.testButton3};
// loop through the array, find the button with respective id and set the listener
for(int i=0; i<ids.length; i++){
Button button = (Button) findViewById(ids[i]);
button.setOnLongClickListener(longClickListener);
}
View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
public boolean onLongClick(View v) {
// .....
return true;
}
};
回答3:
if your buttons are in the LinearLayout, in the RelativeLayour or in the other. And you have very many buttons.
View.OnLongClickListener listener = new View.OnLongClickListener() {
public boolean onLongClick(View v) {
Log.d("ID", ""+v.getId());
switch (v.getId())
{
case R.id.button1:
//do something for button1
break;
case R.id.button2:
//do something for button2
break;
...
}
return true;
}
};
LinearLayout layout=(LinearLayout)findViewById(R.id.IdOfLinearLayout);
for(int i=0;i<layout.getChildCount();i++){
layout.getChildAt(i).setOnLongClickListener(listener);
}
xml file for example...
<LinearLayout
android:id=@+id/IdOfLinearLayout"....>
<Button
android:id="@+id/button1"
android:layout_width=...
android:layout_height=...
.../>
<Button
android:id="@+id/button2"
... />
....
<Button
android:id="@+id/buttonN"
.../>
</LinearLayout>
来源:https://stackoverflow.com/questions/17452030/onlongclick-listener-for-group-of-buttons-in-android