我有一个水平管理器数组,包括editfield,dropdown,label和一个按钮。我将所有这些水平字段管理器数组放在一个垂直管理器中,使表格像网格状结构。我可以做到,但是如果我们专注于水平管理器,那么我想这样做,那么水平管理器中的所有组件都应该提供相同的颜色,我不知道如何制作它。
答案 0 :(得分:3)
如果你在4.5及更低版本,扩展HorizontalFieldManager,添加颜色属性,在paint事件上使用它并在焦点更改时无效:
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进行VISUAL STATE 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);
}
}