我在我的应用中动态显示按钮。当我确定要显示的按钮数量时,它工作正常。但是,现在我想在点击时更改按钮的背景颜色,我也可以这样做,但只有标签部分改变了颜色,其余部分仍然是相同的。
那么,我该如何解决这个问题呢?这是我到目前为止的代码:
btn = new ButtonField[Global.vec_locdisablecityname.size()];
for(int i=0;i<Global.vec_locdisablecityname.size();i++){
btn[i] = new ButtonField("",ButtonField.CONSUME_CLICK |ButtonField.FOCUSABLE);
btn[i].setLabel((String)Global.vec_locdisablecityname.elementAt(i));
if(Global.flag == true){
Global.flag = false;
Bitmap bmp_backbtnclick=Bitmap.getBitmapResource("bg-btn-location.png");
Background bg_backbtnclick=BackgroundFactory.createBitmapBackground(bmp_backbtnclick);
btn[i].setBackground(bg_backbtnclick);
}
else {
if(Global.selectedbutton == i){
Bitmap bmp_backbtnclick=Bitmap.getBitmapResource("bg-btn-location.png");
Background bg_backbtnclick=BackgroundFactory.createBitmapBackground(bmp_backbtnclick);
btn[i].setBackground(bg_backbtnclick);
}
else {
btn[i].setBackground(null);
}
}
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if ( field instanceof ButtonField ) {
for ( int i = 0; i <= 1; i++ ) {
if ( field == btn[i] ) {
System.out.println("CLicked::::---->>>" +i);
btn[i].setBackground(null);
Global.selectedbutton = i;
selectedcityid = Global.vec_locdisablecitycode.elementAt(i).toString();
System.out.println("value of selected city id:::-->>" +selectedcityid);
selectedcitycodeparse(selectedcityid);
selectedstartloading();
break;
}
else {
btn[i].setBackground(null);
}
}
}
}
};
btn[i].setChangeListener(listener);
hfmbuttons.add(btn[i]);
}