2

我想要在文本右侧带有图像和复选框的列表字段。我在左侧获得了图像,但无法在右侧获得复选框。如果可能,复选框应该是不可见的。只应看到刻度线。我想实现

   Image text1 chechbox 
   Image text2 
   Image text3 checkbox            

所有列表项的图像应该相同。

public final class ListfieldDemo1 extends MainScreen

{

private Vector _listElements;
ListField list;
private ListField _checkList;

public ListfieldDemo1()
{        
    // Set the displayed title of the screen   
    super(Manager.NO_VERTICAL_SCROLL);
    add(new LabelField("Fruits List", LabelField.FIELD_HCENTER));
    add(new SeparatorField());
    setTitle("ListField with images and checkbox");
    _listElements = new Vector();
    add(new SeparatorField());
    list = new ListField();
    ListCallback _callback = new ListCallback(this);
    list.setCallback(_callback);
    list.setSize(4);
    int index = list.getSelectedIndex();

    add(list);

    createField();
}

    protected void createField()
    {
        String itemOne = "Apple";
        String itemTwo = "Blackberry";
        String itemthree ="Grapes";
        String itemfour = "Banana";
        _listElements.addElement(itemOne);

        _listElements.addElement(itemTwo);
        _listElements.addElement(itemthree);
        _listElements.addElement(itemfour);
        reloadList();
    }

    private void reloadList() {

        list.setSize(_listElements.size());
    }
    public boolean invokeAction(int action)
    {
        switch (action)
        {
        case ACTION_INVOKE: // Trackball click.
        return true;
        }
        return super.invokeAction(action);

    }
class ListCallback implements ListFieldCallback
  {
private static final int LEFT_OFFSET = 10;
private static final int TOP_OFFSET = 10;
ListfieldDemo1 listfieldDemo1;

public ListCallback(ListfieldDemo1 listfieldDemo1)
{
        this.listfieldDemo1 = listfieldDemo1;
}

public void drawListRow(ListField listField, Graphics g, int index,
        int y, int w) {

    String text = (String)_listElements.elementAt(index);
    g.drawText(text, 60, y + 5, 0, w);
    text = (String) _listElements.elementAt(index);
    Bitmap bitm = Bitmap.getBitmapResource("bullet_arrow.png");
    int xpos = LEFT_OFFSET;
    int ypos = TOP_OFFSET + y;
    w = bitm.getWidth();
    int h = bitm.getHeight();

    g.drawBitmap(xpos, ypos, w, h, bitm, 0, 0);

    xpos = w + 20;


}

public Object get(ListField listField, int index) {
    // TODO Auto-generated method stub
    return null;
}

public int getPreferredWidth(ListField listField) {
    // TODO Auto-generated method stub
    return 0;
}

public int indexOfList(ListField listField, String prefix, int start) {
    // TODO Auto-generated method stub
    return 0;
}

}
}

提前致谢。

4

3 回答 3

7

我认为您想要如下图所示的内容:

用刻度线列出测试

这是代码:

import java.util.Vector;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

public final class ListDemoScreen extends MainScreen {

    private Vector _listElements;

    ListField list;
    private ListField _checkList;
    private MenuItem _toggleItem;

    public ListDemoScreen() {
        super(Manager.NO_VERTICAL_SCROLL);
        // Set the displayed title of the screen
        setTitle("List Demo");
        add(new LabelField("Fruits List", LabelField.FIELD_HCENTER));
        add(new SeparatorField());

        _listElements = new Vector();
        add(new SeparatorField());
        list = new ListField();
        ListCallback _callback = new ListCallback(this);

        list.setCallback(_callback);
        list.setSize(4);

        add(list);

        createField();

    }

    protected void createField() {
        ChecklistData itemOneCheckList = new ChecklistData("Apple", false);
        ChecklistData itemTwoCheckList = new ChecklistData("Blackberry", false);
        ChecklistData itemThreeCheckList = new ChecklistData("Grapes", false);
        ChecklistData itemFourCheckList = new ChecklistData("Banana", false);

        _listElements.addElement(itemOneCheckList);
        _listElements.addElement(itemTwoCheckList);
        _listElements.addElement(itemThreeCheckList);
        _listElements.addElement(itemFourCheckList);
        reloadList();

    }

    private void reloadList() {
        list.setSize(_listElements.size());
    }

    public boolean invokeAction(int action) {
        switch (action) {
        case ACTION_INVOKE: // Trackball click.
            int index = list.getSelectedIndex();
            ChecklistData data = (ChecklistData) _listElements.elementAt(index);
            data.toggleChecked();
            _listElements.setElementAt(data, index);
            list.invalidate(index);
            return true; // We've consumed the event.
        }
        return super.invokeAction(action);

    }

    class ListCallback implements ListFieldCallback {
        ListDemoScreen listDemoScreen;

        public ListCallback(ListDemoScreen listDemoScreen) {
            this.listDemoScreen = listDemoScreen;

        }

        public void drawListRow(ListField list, Graphics g, int index, int y,
                int w) {

            ChecklistData data = (ChecklistData) _listElements.elementAt(index);

            Bitmap bitmapImage = null;
            Bitmap bitmapTick = null;
            int widthImage=0;
            int heightImage=list.getRowHeight(index);
            int widthTick=0;
            int heightTick=list.getRowHeight(index);;
            int maxHeight = list.getRowHeight(index);

            int xpos=0;
            int ypos=y;
            bitmapImage = Bitmap.getBitmapResource("earth-icon.png");
            bitmapTick = Bitmap.getBitmapResource("ok-icon.png");
            if(bitmapImage != null && bitmapTick != null) {
                widthImage = bitmapImage.getWidth();
                widthTick = bitmapTick.getWidth();
                heightImage = bitmapImage.getHeight();
                heightTick = bitmapTick.getHeight();
                maxHeight = (heightTick > heightImage) ? heightTick : heightImage;

                list.setRowHeight(index, maxHeight);

                g.drawBitmap( xpos, ypos+(list.getRowHeight(index)-heightImage)/2, widthImage, heightImage, bitmapImage, 0, 0 );
                if(data.isChecked()) {
                    g.drawBitmap( getWidth()-widthTick-2, ypos+(list.getRowHeight(index)-heightTick)/2, widthTick, heightTick, bitmapTick, 0, 0 );
                }
            }

            ChecklistData currentRow = (ChecklistData) this.get(list, index);

            StringBuffer rowString = new StringBuffer();

            rowString.append(currentRow.getStringVal());
            // Draw the text.
            g.drawText(rowString.toString(), xpos + widthImage, y + (list.getRowHeight(index)-getFont().getHeight())/2, 0, -1);
        }

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

        public int indexOfList(ListField list, String prefix, int string) {
            return _listElements.indexOf(prefix, string);
        }

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

    }

    private class ChecklistData {
        private String _stringVal;
        private boolean _checked;


        ChecklistData(String stringVal, boolean checked) {
            _stringVal = stringVal;
            _checked = checked;
        }

        private String getStringVal() {
            return _stringVal;
        }

        private boolean isChecked() {
            return _checked;
        }


        // Toggle the checked status.
        private void toggleChecked() {
            _checked = !_checked;
        }

    }


    protected void makeMenu(Menu menu, int instance) {
        Field focus = UiApplication.getUiApplication().getActiveScreen()
                .getLeafFieldWithFocus();
        if (focus == _checkList) {
            menu.add(_toggleItem);
        }

        super.makeMenu(menu, instance);
    }

}

使用您自己的图标earth-icon.pngok-icon.png勾选标记)。

于 2012-05-09T18:55:12.890 回答
1

使用此样式位将 Checkboxfield 标签放在左侧。

private static final long checkBoxStyle = 134217728;
add(new CheckboxField("test " , false, checkBoxStyle | USE_ALL_WIDTH));
于 2012-07-02T07:08:59.647 回答
0

我有同样的问题,这是我的解决方案:

public class FlippedCheckboxField extends CheckboxField {
private final boolean rightToLeft;
private int dislocation = -1;

public FlippedCheckboxField(final String arg0, final boolean arg1, final long arg2, final boolean rightToLeft) {
    super(arg0, arg1, arg2);
    this.rightToLeft = rightToLeft;
}

protected void paint(final Graphics graphics) {
    final int width = getWidth();
    final int height = getHeight();
    final Bitmap x = new Bitmap(width, height);
    final Graphics g = Graphics.create(x);
    super.paint(g);

    if (this.dislocation == -1) { // not yet initialized
        this.dislocation = calculateDislocation(width, height, x, this.rightToLeft);
    }

    graphics.drawBitmap(width - this.dislocation, 0, this.dislocation, height, x, 0, 0);
    graphics.drawBitmap(0, 0, width - this.dislocation, height, x, this.dislocation, 0);
}

private int calculateDislocation(final int width, final int height, final Bitmap x, final boolean rightToLeft) {
    final int middleRow[] = new int[width];
    x.getARGB(middleRow, 0, width, 0, height / 2, width, 1); // get middle row
    final int beginCheckbox = findNextColumn(height, x, middleRow, rightToLeft ? width : 0, rightToLeft, false);
    final int endCheckbox = findNextColumn(height, x, middleRow, beginCheckbox, rightToLeft, true);
    final int beginText = findNextColumn(height, x, middleRow, endCheckbox, rightToLeft, false);
    if (rightToLeft) {
        final int i = width - beginCheckbox + beginText;
        return i;
    } else {
        return beginText - beginCheckbox;
    }
}

private int findNextColumn(final int height, final Bitmap x, final int[] middleRow, final int xOffset, final boolean rightToLeft, final boolean findWhite) {
    final int whitePixel = middleRow[rightToLeft ? 0 : middleRow.length - 1];
    if (rightToLeft) {
        for (int i = xOffset - 1; i >= 0 ; i --) {
            final boolean isColumnWhite = isColumnWhite(height, x, middleRow, i, whitePixel);
            if (isColumnWhite ^ !findWhite) { // found first white or non-white column, return
                return i;
            }
        }
        return xOffset;
    } else {
        for (int i = xOffset; i < middleRow.length; i ++) {
            final boolean isColumnWhite = isColumnWhite(height, x, middleRow, i, whitePixel);
            if (isColumnWhite ^ !findWhite) { // found first white or non-white column, return
                return i;
            }
        }
        return xOffset;
    }
}

private boolean isColumnWhite(final int height, final Bitmap x, final int[] middleRow, final int xOffset, final int whitePixel) {
    if (middleRow[xOffset] != whitePixel) {
        return false;
    } else {
        final int currentColumn[] = new int[height]; // get current column
        x.getARGB(currentColumn, 0, 1, xOffset, 0, 1, height);
        for (int i = 0; i < height; i++) { // check if all pixels in the current column are white
            if (currentColumn[i] != whitePixel) { // non white
                return false;
            }
        }
        return true;
    }
}
}

在此处查看原始问题:在 Blackberry 上将复选框向右对齐

于 2012-05-09T15:19:14.960 回答