问题
Here's my situation: I programmatically give some buttons an onClickListener, however, I can't fully-handle this event, because I'd like to detect which button was pressed to give to that button (when it is pressed) a new value. Here's how I program those buttons:
final View.OnClickListener soundButtonListener = new View.OnClickListener() {
public void onClick(View v) {
playSound(1);
int x = songPlay * songProgress;
mplayer.seekTo(x);
}
};
tableLayout = (TableLayout)v
.findViewById(R.id.tableLayout);
int number = 1;
for (int i = 0; i < tableLayout.getChildCount(); i++) {
TableRow row = (TableRow)tableLayout.getChildAt(i);
for (int j = 0; j < row.getChildCount(); j++) {
Button button = (Button) row.getChildAt(j);
button.setText("" + number);
button.setOnClickListener(soundButtonListener);
songProgress = j;
number++;
}
}
As you see, they haven't a click listener for each one, so when I press my button, mplayer always seekto a point, becausae songProgress stops at 64 (I have 64 buttons to handle). What is good is that I have a number for each button (as you see button.setText("" + number);), and I'd like to check what is their number to give to songProgress different values.
回答1:
If i correctly understand you can do something like this:
final View.OnClickListener soundButtonOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v instanceof Button) {
Button button = (Button) v;
String text = button.getText().toString();
if(text == "1") {
//...
} else if(text == "2") {
}
//OR
switch (text) {
case "1": {
break;
}
case "2": {
break;
}
//...
}
}
}
};
But in my opinion better use tag instead of text:
//set tag for your button
button.setTag(number);
//use this tag
Integer number = (Integer) v.getTag();
if(number != null) {
//...
}
回答2:
Your onClick
has a View v
Object which should be the view that triggered the event. So this view should be your button.
Now you could get the text of that button with v.getText()
. Then parse the text to your number and use it.
回答3:
If u want to know the text of the pressed button, use this:
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();
//Do your thing with buttonText
}
回答4:
It's very simple to detect your button. Simply you can do like
button.setId(2000 + 0);
for reference you can check it -
for (int i = 0; i < ch.length; i++) {
Button button = new Button(this);
button.setTextColor(context.getResources().getColor(R.color.black));
button.setPadding(5, 5, 5, 5);
if (Integer.parseInt(String.valueOf(ch[i])) == 0) {
button.setText("No");
button.setId(2000 + 0);
layout.addView(button, layoutParams);
button.setOnClickListener(this);
}
if (Integer.parseInt(String.valueOf(ch[i])) == 1) {
button.setText("Less");
button.setId(2000 + 1);
layout.addView(button, layoutParams);
button.setOnClickListener(this);
}
if (Integer.parseInt(String.valueOf(ch[i])) == 2) {
button.setText("Half");
button.setId(2000 + 2);
layout.addView(button, layoutParams);
button.setOnClickListener(this);
}
}
and in onClick
@Override
public void onClick(View v) {
switch (v.getId()) {
case 2000:
Log.e(TAG,"button 0 clicked");
break;
case 2001:
Log.e(TAG,"button 1 clicked");
break;
case 2002:
Log.e(TAG,"button 2 clicked");
break;
case 2003:
Log.e(TAG,"button 3 clicked");
break;
}
}
来源:https://stackoverflow.com/questions/37785204/detect-which-button-was-pressed