从动态的观点认识一个按钮 [英] Recognizing a button from a dynamic view
问题描述
我写了这个code的动态布局,其中我使用这个循环,产生一对按钮(这是code,我生成它们的一部分)
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);
}
我希望做的是,如果点击按钮1,按钮2应该得到启用(最初它被禁用)。
What I wish to do is if button1 is clicked, button2 should get enabled (initially it is disabled).
这将是一个非常容易的事,如果按钮是在XML创建为那么他们将不得不为每个单独R.id.xxxxx名字做的,但在这里我无法理解如何检测的OnClick另一个按钮(视图v)方法,如果启用与否,我试图添加标记为每个按钮,这样我还有另外一个参数来识别按钮,但我不知道如何与识别的其他按钮我可以改变鉴于点击按钮1的信息。
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.
推荐答案
我假设你正在使用的按钮标签在点击处理。为了保持标签数据,并添加按钮之间的布线需要,您可以创建一个数据结构,将作为一个标记:
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;
}
}
然后,你可以重新组织设置code:
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);
}
的点击处理显然需要改变投的 getTag()
到 ButtonTag
代替字符串
。
如果你不需要的年龄和国家的信息来区分按钮类型,只需设置每个按钮为其他的标记。
If you don't need the "age" and "country" information to distinguish button types, just set each button as the tag for the other.
编辑:
对于后者方案中,这里是你将如何在点击监听器使用此:
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);
}
}
如果您所需要的年龄和其他原因标签的国家的一部分,code将是只有一点点不同:
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);
}
}
这篇关于从动态的观点认识一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!