我有一个水平字段数组,每个字段包含一个位图和一个labelfield。整行应该是可点击的,到目前为止工作,但我怎样才能正确设置焦点颜色?目前,onFocus和onUnfocus功能完全被忽略。
这是我的数组的定义:
for (int i = 0; i < listSize; i++) {
logInDetailManager[i] = new HorizontalFieldManager(
Manager.USE_ALL_WIDTH | Field.FOCUSABLE) {
protected void onFocus(int direction) {
super.onFocus(direction);
background_color = Color.RED;
invalidate();
}
protected void onUnfocus() {
invalidate();
background_color = Color.GREEN;
}
这就是我添加水平字段的方式:
logInDetailManager[i].setChangeListener(this);
logInDetailManager[i].add(dummyIcon[i]);
logInDetailManager[i].add(new LabelField("hello"));
logInDetailManager[i].add(new NullField(Field.FOCUSABLE));
add(logInDetailManager[i]);
答案 0 :(得分:0)
抱歉,由于我是Stackoverflow的新手,我昨天无法对自己的帖子发表评论;) 这就是我解决它的方法:
我从HFM中删除了onFocus()和onUnfocus(),并在paint方法中设置了背景颜色,以便在聚焦时更改整行颜色:
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(isFocus() ? Color.RED : Color.GREEN);
graphics.clear();
invalidate();
super.paint(graphics);
}
我还发现,如果你想设置更复杂的背景(即使用渐变),你也可以使用setBackground(int visual,Background background)方法:
Background bg_focus = (BackgroundFactory
.createLinearGradientBackground(Color.GREEN, Color.LIGHTGREEN,
Color.LIGHTGREEN, Color.GREEN));
loginDetailManager[i].setBackground(VISUAL_STATE_FOCUS, bg_focus);
确保在使用setBackground函数时删除你的paint方法!