本文介绍了子字段上水平经理重点黑莓变色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有水平管理器的阵列,其包括EditField中,下拉菜单,标签和按钮。我把所有这些阵列horizo​​ntalfield经理在一个垂直管理员做出象表状结构网格。我可以做,但我想这样做,如果我们专注于水平经理,然后在横向管理的所有组件应该给出一个相同的颜色,我不知道如何使这一点。

I have an array of horizontal manager which consist of editfield, dropdown, label and a button. I place all these array of horizontalfield manager in one vertical manager to make a table like grid like structure. I can make it but I want to do that if we get focus on horizontal manager then all components in the horizontal manager should give a same color I don't know how to make this.

推荐答案

如果您在4.5和更低,延长Horizo​​ntalFieldManager,添加颜色属性,使用它的绘制事件和无效的焦点变化:

If you in 4.5 and lower, extend HorizontalFieldManager, add color property, use it on paint event and invalidate on focus change:

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:

If you in 4.6 and higher, use setBackground for 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();
    }
}

这是,如果你想突出显示所有控件(按钮,复选框,选择和编辑)的情况,如果水平经理的重点是:

And here is a case if you want to highlight all controls (button, checkbox, choice and edit) if horizontal manager is focused:

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);
    }
}

这篇关于子字段上水平经理重点黑莓变色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:25