问题
I have written this code for a dynamic layout where I am using this loop to generate a pair of buttons (this is the part of code where I generate them)
for(int i = 1; i <= 2 ; i++) {
Button button1 = new Button(this);
button1.setTag("age");
button1.setId(i);
layout.addView(button1);
Button button2 = new Button(this);
button2.setId(i);
button2.setTag("country");
button2.setEnabled(false);
layout.addView(button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
What I wish to do is if button1 is clicked, button2 should get enabled (initially it is disabled).
This would be a very easy task to do if the buttons were created in xml as then they will have separate R.id.xxxxx names for each, but here I am unable to understand how to detect the other button in the OnClick(View v) method so that I can change if it is enabled or not, I have tried to add the tag for each button so that I have another parameter to recognize the buttons but I have no idea how to recognize the other button with the view information of the clicked button1.
回答1:
I assume that you are using the button tags in your click processing. To keep the tag data and add the needed wiring between buttons, you can create a data structure that would serve as a tag:
static class ButtonTag {
String buttonType;
Button partner;
ButtonTag(String type, Button button) {
buttonType = type;
partner = button;
}
}
Then you could reorganize your setup code:
for(int i = 1; i <= 2 ; i++) {
Button button1 = new Button(this);
button1.setId(i);
layout.addView(button1);
Button button2 = new Button(this);
button2.setId(i);
button2.setEnabled(false);
button1.setTag(new ButtonTag("age", button2));
button2.setTag(new ButtonTag("country", button1));
layout.addView(button2);
}
The click processing will obviously need to be changed to cast getTag() to a ButtonTag instead of a String.
If you don't need the "age" and "country" information to distinguish button types, just set each button as the tag for the other.
EDIT:
With the latter scheme, here's how you would use this in a click listener:
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof Button) {
Button btn = (Button) tag;
btn.setEnabled(true);
v.setEnabled(false);
}
}
If you needed the "age" and "country" part of the tag for other reasons, the code would be only a little different:
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof ButtonTag) {
ButtonTag bTag = (ButtonTag) tag;
bTag.partner.setEnabled(true);
v.setEnabled(false);
}
}
回答2:
I got a solution to the problem after referring to this question here ( find button by ID or TAG ), it solves the problem I was facing as such !
public class DynmaicViewExperimentActivity extends Activity implements OnClickListener{
List<Button> buttons;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buttons = new ArrayList<Button>();
setDynamicContentViewOfThisPage();
}
public void onClick(View v) {
// TODO Auto-generated method stub
int buttonType = 0;
if (v.getTag()=="age")
buttonType = 1;
else if (v.getTag()=="country")
buttonType = 2;
switch (buttonType) {
case 1:
for(Button b: buttons) {
if(b.getId() == v.getId() && b.getTag().equals("country")){
b.setEnabled(true);
v.setEnabled(false);
}
case 2:
for(Button b: buttons) {
if(b.getId() == v.getId() && b.getTag().equals("age")){
b.setEnabled(true);
v.setEnabled(false);
}
}
}
}
private void setDynamicContentViewOfThisPage() {
// Defining the Scroll View and the LinearLayout
ScrollView sv = new ScrollView(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);
for(int i = 1; i <= 2 ; i++) {
Button button1 = new Button(this);
button1.setId(i);
button1.setTag("age");
buttons.add(button1);
l.addView(button1);
Button button2 = new Button(this);
button2.setId(i);
button2.setTag("country");
buttons.add(button2);
l.addView(button2);
button.setOnClickListener(this);
button2.setOnClickListener(this);
// Set the content View to this
this.setContentView(sv);
}
}
}
来源:https://stackoverflow.com/questions/10890100/recognizing-a-button-from-a-dynamic-view