0
我有一個由editfield,下拉菜單,標籤和一個按鈕組成的水平管理器陣列。我將所有這些水平域管理器放在一個垂直管理器中,以製作一個像網格一樣的表格。我可以做到,但如果我們專注於水平管理器,那麼我想這樣做,那麼橫向經理中的所有組件都應該給出相同的顏色,我不知道如何做到這一點。黑莓在水平管理器焦點上改變子域的顏色
我有一個由editfield,下拉菜單,標籤和一個按鈕組成的水平管理器陣列。我將所有這些水平域管理器放在一個垂直管理器中,以製作一個像網格一樣的表格。我可以做到,但如果我們專注於水平管理器,那麼我想這樣做,那麼橫向經理中的所有組件都應該給出相同的顏色,我不知道如何做到這一點。黑莓在水平管理器焦點上改變子域的顏色
如果在4.5和更低,延長HorizontalFieldManager,添加顏色屬性,使用油漆事件和無效的焦點變化:
class Scr extends MainScreen {
HorizontalFieldManager mMainPanel;
VerticalFieldManager mVerticalPanel;
public Scr() {
mMainPanel = new HorizontalFieldManager();
add(mMainPanel);
mVerticalPanel = new VerticalFieldManager(USE_ALL_HEIGHT
| USE_ALL_WIDTH);
mMainPanel.add(mVerticalPanel);
for (int i = 0; i < 5; i++) {
HFMHighlight hfm = new HFMHighlight();
hfm.setHightlightColor(Color.GRAY);
hfm.add(new LabelField("Label " + i, FIELD_LEFT));
hfm.add(new BasicEditField(FIELD_RIGHT));
mVerticalPanel.add(hfm);
}
}
}
class HFMHighlight extends HorizontalFieldManager {
int mHColor = -1;
public void setHightlightColor(int color) {
mHColor = color;
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (-1 != mHColor && isFocus()) {
graphics.setBackgroundColor(mHColor);
graphics.clear();
}
super.paint(graphics);
}
}
如果您在4.6及更高版本,使用的setBackground爲可視狀態FOCUS:
class Scr extends MainScreen {
HorizontalFieldManager mMainPanel;
VerticalFieldManager mVerticalPanel;
public Scr() {
RadioInfo.isDataServiceOperational();
CoverageInfo.isOutOfCoverage();
WLANInfo.getWLANState();
mMainPanel = new HorizontalFieldManager();
add(mMainPanel);
mVerticalPanel = new VerticalFieldManager(USE_ALL_HEIGHT
| USE_ALL_WIDTH);
mMainPanel.add(mVerticalPanel);
for (int i = 0; i < 5; i++) {
HFMHighlight hfm = new HFMHighlight(Color.GRAY);
hfm.add(new LabelField("Label " + i, FIELD_LEFT));
hfm.add(new BasicEditField(FIELD_RIGHT));
mVerticalPanel.add(hfm);
}
}
}
class HFMHighlight extends HorizontalFieldManager {
public HFMHighlight(int color) {
Background focusedBG = BackgroundFactory.createSolidBackground(color);
setBackground(VISUAL_STATE_FOCUS, focusedBG);
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
}
這裏是如果你想突出,如果水平經理集中所有控件(按鈕,複選框,選擇和編輯)的情況下:
class Scr extends MainScreen {
HorizontalFieldManager mMainPanel;
VerticalFieldManager mVerticalPanel;
public Scr() {
mMainPanel = new HorizontalFieldManager();
add(mMainPanel);
mVerticalPanel = new VerticalFieldManager(USE_ALL_HEIGHT
| USE_ALL_WIDTH);
mMainPanel.add(mVerticalPanel);
for (int i = 0; i < 5; i++) {
HHorizontalFieldManager hfm = new HHorizontalFieldManager();
mVerticalPanel.add(hfm);
HButtonField button = new HButtonField("btn");
button.setHightlightColor(Color.GRAY);
hfm.add(button);
HCheckboxField checkbox = new HCheckboxField("chk box", false);
checkbox.setHightlightColor(Color.GRAY);
hfm.add(checkbox);
String choiceItems[] = { "one", "two", "three" };
HObjectChoiceField dropdown = new HObjectChoiceField("choice",
choiceItems);
dropdown.setHightlightColor(Color.GRAY);
hfm.add(dropdown);
HEditField edit = new HEditField("edit", "___");
edit.setHightlightColor(Color.GRAY);
hfm.add(edit);
}
}
}
class HHorizontalFieldManager extends HorizontalFieldManager {
public HHorizontalFieldManager() {
super();
}
public HHorizontalFieldManager(long style) {
super(style);
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (isFocus()) {
graphics.setBackgroundColor(Color.GRAY);
graphics.clear();
}
super.paint(graphics);
}
}
class HEditField extends EditField {
public HEditField() {
super();
}
public HEditField(long style) {
super(style);
}
public HEditField(String label, String initialValue, int maxNumChars,
long style) {
super(label, initialValue, maxNumChars, style);
}
public HEditField(String label, String initialValue) {
super(label, initialValue);
}
private int mHColor = -1;
public void setHightlightColor(int color) {
mHColor = color;
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (-1 != mHColor && getManager().isFocus()) {
graphics.setBackgroundColor(mHColor);
graphics.clear();
}
super.paint(graphics);
}
}
class HButtonField extends ButtonField {
public HButtonField() {
super();
}
public HButtonField(long style) {
super(style);
}
public HButtonField(String label, long style) {
super(label, style);
}
public HButtonField(String label) {
super(label);
}
private int mHColor = -1;
public void setHightlightColor(int color) {
mHColor = color;
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (-1 != mHColor && getManager().isFocus()) {
graphics.setBackgroundColor(mHColor);
graphics.clear();
}
super.paint(graphics);
}
}
class HCheckboxField extends CheckboxField {
public HCheckboxField() {
super();
}
public HCheckboxField(String label, boolean checked, long style) {
super(label, checked, style);
}
public HCheckboxField(String label, boolean checked) {
super(label, checked);
}
private int mHColor = -1;
public void setHightlightColor(int color) {
mHColor = color;
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (-1 != mHColor && getManager().isFocus()) {
graphics.setBackgroundColor(mHColor);
graphics.clear();
}
super.paint(graphics);
}
}
class HObjectChoiceField extends ObjectChoiceField {
public HObjectChoiceField() {
super();
}
public HObjectChoiceField(String label, Object[] choices,
int initialIndex, long style) {
super(label, choices, initialIndex, style);
}
public HObjectChoiceField(String label, Object[] choices,
int initialIndex) {
super(label, choices, initialIndex);
}
public HObjectChoiceField(String label, Object[] choices,
Object initialObject) {
super(label, choices, initialObject);
}
public HObjectChoiceField(String label, Object[] choices) {
super(label, choices);
}
private int mHColor = -1;
public void setHightlightColor(int color) {
mHColor = color;
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public void paint(Graphics graphics) {
if (-1 != mHColor && getManager().isFocus()) {
graphics.setBackgroundColor(mHColor);
graphics.clear();
}
super.paint(graphics);
}
}
嗨,感謝您的幫助,但是當我使用我的代碼時,我只能更改水平管理器顏色,沒有組件顏色更改我想更改所有組件的顏色,當聚焦增益更改爲水平管理器時也會更改。 – Kumar 2009-09-01 12:08:41
不客氣!我認爲,只有顏色管理員纔夠好。但是如果你想爲每個領域着色,你將不得不告訴我你使用的是什麼領域?事情是我們必須做我對經理人員內部每個領域所做的事情。所以我們必須爲每一種領域實現自己的類。 – 2009-09-01 12:29:04
我使用editfield,buttonfield,dropdownfield和checkboxfield這些是我在管理器中使用的字段。 – Kumar 2009-09-01 12:56:32