我有一个重写的LabelField,让我改变根据我在ListField项目是否应该被制服还是现在的字体颜色。使得色彩的LabelField制服的伟大工程。但是,当行(包含我的LabelField)在ListField高亮显示时,我想在标签字段颜色是不同的或倒置。

下面是我的代码:

public class MyLabelField extends LabelField{

public MyLabelField (String s, long l){
    super(s, l);
    m_bSubdue = true; // this value does change in implementation but not in this sample
}

protected void paint(Graphics g)
{
    if(m_bSubdue && !this.isFocus()){  // this isFocus() trick is not working
        int color = 0x00696969; // RGB val
        g.setColor(color);
    }
    super.paint(g);
}

boolean m_bSubdue;

}

在此示例中,我想MyLabelField与灰色绘制但当ListField行其载于具有焦点,我想的颜色默认为的LabelField涂料,应该使它白色。

根据测试我的代码,它似乎将labelField没有得到焦点时,它的父行具有焦点。也许需要在其他地方在我的代码改变...

有帮助吗?

解决方案

要切换的的LabelField颜色,如果它的选择,我们可以检查它的油漆()方法。但即使如此,我们将不得不重新绘制每一行中ListField。而这就是问题所在:ListField的每次行选择改变时,只有两个ListFieldCallback.drawRow()方法调用 - 首先为先前选定行(仍处于选中状态)和第二新选择行(处于选择状态的话) ...

我试图保存以前选择的行索引和重绘上drawRow()的每一个呼叫,但是由于某种原因LabelField.paint()方法未能然后触发。

所以我来用简单的,不知何故难看溶液:时间表ListField.invalidate()的TimerTask。期间100毫秒就足够了,并没有击中性能为好。

杰森Emerick的代码我已经用作引导到延伸ListField。

class LabelListField extends ListField implements ListFieldCallback {
    private Vector mValues;
    private Vector mRows;

    public LabelListField(Vector values) {
        super(0);
        setRowHeight(70);
        setCallback(this);

        mValues = values;
        fillListWithValues(values);

        scheduleInvalidate();
    }

    private void scheduleInvalidate() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                invalidate();
            }
        }, 0, 100);
    }

    private void fillListWithValues(Vector values) {
        mRows = new Vector();
        for (int i = 0; i < values.size(); i++) {
            TableRowManager row = new TableRowManager();
            String title = "Item# " + String.valueOf(i + 1);
            LabelField titleLabel = new LabelField(title);
            titleLabel.setFont(Font.getDefault().derive(Font.BOLD));
            row.add(titleLabel);
            String value = (String) values.elementAt(i);
            ListLabel valueLabel = new ListLabel(this, i, value);
            valueLabel.setFont(Font.getDefault().derive(Font.ITALIC));
            row.add(valueLabel);
            mRows.addElement(row);
        }
        setSize(mRows.size());
    }

    private class TableRowManager extends Manager {
        public TableRowManager() {
            super(0);
        }

        public void drawRow(Graphics g, int x, int y, 
            int width, int height) {
            layout(width, height);
            setPosition(x, y);
            g.pushRegion(getExtent());
            paintChild(g, getField(0));
            paintChild(g, getField(1));
            g.popContext();
        }

        protected void sublayout(int width, int height) {
            int fontHeight = Font.getDefault().getHeight();
            int preferredWidth = getPreferredWidth();
            Field field = getField(0);
            layoutChild(field, preferredWidth - 5, fontHeight + 1);
            setPositionChild(field, 5, 3);
            field = getField(1);
            layoutChild(field, preferredWidth - 5, fontHeight + 1);
            setPositionChild(field, 5, fontHeight + 6);
            setExtent(preferredWidth, getPreferredHeight());
        }

        public int getPreferredWidth() {
            return Display.getWidth();
        }

        public int getPreferredHeight() {
            return getRowHeight();
        }
    }

    public void drawListRow(ListField listField, Graphics g, 
            int index, int y, int width) {
        LabelListField list = (LabelListField) listField;
        TableRowManager rowManager = (TableRowManager) list.mRows
                .elementAt(index);
        rowManager.drawRow(g, 0, y, width, list.getRowHeight());

    }

    public Object get(ListField list, int index) {
        return mValues.elementAt(index);
    }

    public int indexOfList(ListField list, String prefix, int start) {
        for (int x = start; x < mValues.size(); ++x) {
            String value = (String) mValues.elementAt(x);
            if (value.startsWith(prefix)) {
                return x;
            }
        }
        return -1;
    }

    public int getPreferredWidth(ListField list) {
        return Display.getWidth();
    }

    class ListLabel extends LabelField {
        int mIndex = -1;

        public ListLabel(LabelListField list, int index, String text) {
            super(text);
            mIndex = index;
        }

        protected void paint(Graphics graphics) {
            if (getSelectedIndex() == mIndex)
                graphics.setColor(Color.RED);
            else
                graphics.setColor(Color.BLUE);
            super.paint(graphics);
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top