كيف يمكنني ملون نص Labelfield داخل قائمة قائمة عندما يكون لدى المدير الأصل التركيز؟

StackOverflow https://stackoverflow.com/questions/1267842

سؤال

لدي ArtiveDden Labelfield التي تتيح لي تغيير لون الخط بناء على ما إذا كان ينبغي إخضاع عنصر في قائمة قوتي أو الآن. جعل لون Labelfield ضعيف يعمل بشكل رائع. ولكن، عندما يتم تمييز الصف (الذي يحتوي على Labelfield My 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، يمكننا التحقق مما إذا كانت محددة في طريقة الطلاء (). ولكن حتى ذلك الحين سنضطر إلى إعادة رسم كل صف في قائمة الأدوات. وهذا هو المشكلة: في كل مرة يتم تغيير الاختيار صف قائمة الأدوات الإضافية، لا يوجد سوى مكالمتين فقط من ListFieldCallBack.drawrow () الطريقة الأولى للصف السابق المحدد (لا يزال في حالة مختارة) والثاني للصف الجديد المحدد (بالفعل في حالة مختارة) ...

لقد حاولت حفظ فهرس الصف المحدد السابق وإعادة رسمه على كل مكالمة ذات دعوة ()، ولكن من قبل بعض الأسباب LabelField.paint () لم يتم تشغيل الأسلوب بعد ذلك.

لذلك جئت مع الحل القبيح بسيط بطريقة ما: جدولة Listfield.invalidate () Timertask. الفترة من 100 مللي أم لا يكفي ولا تصل إلى الأداء كذلك.

رمز جيسون Emerick الذي استخدمته كدليل لمقدمي الموجات.

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