for
同じテキスト値で ButtonFields を作成するループがあります。for
これらのボタンのそれぞれから、ループのどのインデックスがボタンを作成したかを示す個別のイベントを取得したいと考えています。ButtonField ごとに匿名クラスを作成したくありません。
2 に答える
3
If they are going one by one (that I'm assuming from your post) you could remember index of the first one in use next code in your fieldChanged
method:
if (field instanceof ButtonField) {
int buttonIndex = field.getManager().getFieldIndex(field) - zeroButtonInex;
}
Don't forget to assign FieldChangeListener
to each of these buttons.
Or sure you could make your new class from ButtonField
(could by anonymous) where you could save index and have getter for it.
于 2012-08-29T06:29:42.530 に答える
3
You have add the buttons to an array. I will give you an idea to try this:
private ButtonField buttonsObj[];
In your code before the for
loop you know the number of buttons, so you can initialize the array length.
int size = 10;
buttonsObj = new ButtonFields[size];
for(int i = 0; i < size; i++)
{
buttonsObj[i] = new ButtonFields["btn"];
buttonsObj[i].setChangeListener(this);
add(buttonsObj[i]);
}
public void fieldChanged(Field field, int context) {
for(int i=0;i<size;i++) {
if(field == buttonsObj[i]) {
// you can trigger your event
}
}
}
于 2012-08-29T06:30:08.267 に答える