Blackberry 开发,添加动态数量的按钮并为其设置 Fieldchangelistener()
我在 Blackberry 应用程序中添加动态数量的按钮,并尝试在每次单击按钮时获取不同的事件。我无法对这些按钮数组应用 setchangelistener() ,因为在添加所有按钮后循环完成后,只会为最后一个索引按钮生成事件。
如果我使用 getIndex(),只有当我不在屏幕上添加任何其他字段时它才能正常运行,但是如果我添加其他字段以及这些按钮数组,则 getIndex() 函数也会将它们计入索引中。
任何人都可以帮我将 FieldChangeListener 设置为 ButtonField 数组吗?
这是我使用 ButtonField 数组的方式的示例代码。我在屏幕顶部添加了两个测试 LabelField 的代码,如果删除它们,代码将运行良好,每次单击按钮我都会得到不同的结果,但如果我保持它们处于活动状态,索引就会生效并且按钮不会工作。
package buttonclickloop;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class ButtoncClickLoop extends MainScreen {
int i = 0;
private ButtonField[] btn = new ButtonField[50];
public ButtoncClickLoop() {
// TODO Auto-generated constructor stub
LabelField Field1 = new LabelField("Field1");
LabelField Field2 = new LabelField("Field2",DrawStyle.RIGHT | ButtonField.USE_ALL_WIDTH);
HorizontalFieldManager FieldHmgr = new HorizontalFieldManager();
FieldHmgr.add(Field1);
FieldHmgr.add(Field2);
FieldHmgr.setMargin(0,0,10,5);
add(FieldHmgr);
while (i < 3){
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
context = field.getIndex();
if (field == btn[context]){
add(new LabelField("Label" + context + ""));
}
}
};
btn[i] = new ButtonField("Button" + i + "");
btn[i].setChangeListener(listener);
add(btn[i]);
i = i + 1;
}
}
}
谢谢, 尼凯什
I am adding dynamic number of buttons in a Blackberry application and trying to get different events on each button click. I am not able to apply the setchangelistener() for these array of buttons as once the loop finishes after adding all the buttons, the events gets generated only for the last indexed button.
If I make us of getIndex(), it runs fine only if I am not adding any other fields on my screen, but if I add other fields along with these array of buttons, the getIndex() function count them in the indexing as well.
Can anyone please help me out in setting FieldChangeListener to array of ButtonField?
Here is a sample code of the way I am using the array of ButtonField. I the code I have added two test LabelField at the top of the screen, if I remove them the code will run fine and I will get different result for each button click, but if I keep them active, the indexing gets effected and the Button wont work.
package buttonclickloop;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class ButtoncClickLoop extends MainScreen {
int i = 0;
private ButtonField[] btn = new ButtonField[50];
public ButtoncClickLoop() {
// TODO Auto-generated constructor stub
LabelField Field1 = new LabelField("Field1");
LabelField Field2 = new LabelField("Field2",DrawStyle.RIGHT | ButtonField.USE_ALL_WIDTH);
HorizontalFieldManager FieldHmgr = new HorizontalFieldManager();
FieldHmgr.add(Field1);
FieldHmgr.add(Field2);
FieldHmgr.setMargin(0,0,10,5);
add(FieldHmgr);
while (i < 3){
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
context = field.getIndex();
if (field == btn[context]){
add(new LabelField("Label" + context + ""));
}
}
};
btn[i] = new ButtonField("Button" + i + "");
btn[i].setChangeListener(listener);
add(btn[i]);
i = i + 1;
}
}
}
Thanks,
Nikesh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从代码中删除 if (field == btn[context]) 。它将起作用。
现在就享受吧。
Remove if (field == btn[context]) from your code .It will work.
Enjoy now.
使用下面的监听器你将获得选定的字段
Use the below listener u will get the selected field