1

我想将焦点放在列表字段的第一个元素或项目上。问题是我从其他字段(不在列表字段中)设置焦点。

protected boolean navigationMovement(int dx, int dy, int status, int time) {

    while (dy > 0) {
    // How to jump into listfield?
        Field f = ???
        if (f.isFocusable()) {
            f.setFocus();
            dy--;
        }
    }
}

这是我的 Listfield 课程。

public class Custom_ListField extends ListField {

private String[] title, category, date, imagepath;
private int[] newsid, catsid;
private List_News newslist;
private Bitmap imagebitmap[], localimage = Config_GlobalFunction
        .Bitmap("image_base.png");
private BrowserField webpage;
private boolean islatest;

private Vector content = null, text;
private ListCallback callback = null;

private int currentPosition = 0, j;
private Util_LazyLoader loader;

public Custom_ListField(Vector content, boolean islatest) {
    this.content = content;
    this.islatest = islatest;

    newsid = new int[content.size()];
    title = new String[content.size()];
    category = new String[content.size()];
    date = new String[content.size()];
    imagepath = new String[content.size()];
    catsid = new int[content.size()];
    imagebitmap = new Bitmap[content.size()];

    for (int i = 0; i < content.size(); i++) {
        newslist = (List_News) content.elementAt(i);
        newsid[i] = newslist.getID();
        title[i] = newslist.getNtitle();
        category[i] = newslist.getNewCatName();
        date[i] = newslist.getNArticalD();
        imagepath[i] = newslist.getImagePath();
        catsid[i] = newslist.getCatID();

        if (!imagepath[i].toString().equals("no picture"))
            imagebitmap[i] = localimage;
    }

    initCallbackListening();

    Main.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            for (j = 0; j < imagepath.length; j++) {
                if (!imagepath[j].toString().equals("no picture")
                        && Config_GlobalFunction.isConnected()) {
                    loader = new Util_LazyLoader(imagepath[j],
                            new Util_BitmapDowloadListener() {
                                public void ImageDownloadCompleted(
                                        Bitmap bmp) {
                                    imagebitmap[j] = bmp;
                                    invalidate();
                                }
                            });
                    loader.run();
                }
            }
        }
    }, 500, false);
}

private void initCallbackListening() {
    callback = new ListCallback();
    this.setCallback(callback);
    this.setRowHeight(-2);      
}

private class ListCallback implements ListFieldCallback {
    public ListCallback() {
    }

    public void drawListRow(ListField listField, Graphics graphics,
            final int index, int y, int width) {
        currentPosition = index;

        if (!imagepath[index].toString().equals("no picture")) {
            float ratio = (float) ((float) localimage.getHeight() / (float) imagebitmap[index]
                    .getHeight());
            Bitmap temp = new Bitmap(
                    (int) (imagebitmap[index].getWidth() * ratio),
                    (int) (imagebitmap[index].getHeight() * ratio));
            imagebitmap[index].scaleInto(temp, Bitmap.FILTER_BILINEAR,
                    Bitmap.SCALE_TO_FIT);
            imagebitmap[index] = temp;

            graphics.drawBitmap(
                    Display.getWidth()
                            - localimage.getWidth()
                            - 5
                            + ((localimage.getWidth() - imagebitmap[index]
                                    .getWidth()) / 2),
                    y
                            + (listField.getRowHeight(index) - imagebitmap[index]
                                    .getHeight()) / 2,
                    imagebitmap[index].getWidth(),
                    imagebitmap[index].getHeight(), imagebitmap[index], 0,
                    0);

            graphics.setColor(Color.BLACK);
            text = Config_GlobalFunction
                    .wrap(title[index], Display.getWidth()
                            - imagebitmap[index].getWidth() - 15);

            for (int i = 0; i < text.size(); i++) {
                int liney = y + (i * Font.getDefault().getHeight());
                graphics.drawText(
                        (String) text.elementAt(i),
                        5,
                        liney + 3,
                        DrawStyle.TOP | DrawStyle.LEFT | DrawStyle.ELLIPSIS,
                        Display.getWidth() - imagebitmap[index].getWidth()
                                - 10);
            }
        } else {
            graphics.setColor(Color.BLACK);
            text = Config_GlobalFunction.wrap(title[index],
                    Display.getWidth() - 10);
            for (int i = 0; i < text.size(); i++) {
                int liney = y + (i * Font.getDefault().getHeight());
                graphics.drawText(
                        (String) text.elementAt(i),
                        5,
                        liney + 3,
                        DrawStyle.TOP | DrawStyle.LEFT | DrawStyle.ELLIPSIS,
                        Display.getWidth() - 10);
            }
        }

        if (text.size() == 2) {
            graphics.setColor(Color.GRAY);
            graphics.drawText(date[index], 5, y
                    + Font.getDefault().getHeight() + 3);

            if (islatest) {
                graphics.setColor(Color.RED);
                graphics.drawText(category[index], Font.getDefault()
                        .getAdvance(date[index]) + 15, y
                        + Font.getDefault().getHeight() + 3);
            }
        } else if (text.size() == 3) {
            graphics.setColor(Color.GRAY);
            graphics.drawText(date[index], 5, y
                    + Font.getDefault().getHeight() * 2 + 3);

            if (islatest) {
                graphics.setColor(Color.RED);
                graphics.drawText(category[index], Font.getDefault()
                        .getAdvance(date[index]) + 15, y
                        + Font.getDefault().getHeight() * 2 + 3);
            }
        }

        if (!imagepath[index].toString().equals("no picture"))
            setRowHeight(index, imagebitmap[index].getHeight() + 10);
        else {
            if (text.size() == 2)
                setRowHeight(index, getRowHeight() + 9);
            else if (text.size() == 3) {
                setRowHeight(index, getRowHeight() * 15 / 10 + 9);
            }
        }

        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, y, width, listField.getRowHeight(index));
    }

    public Object get(ListField listField, int index) {
        return content.elementAt(index);
    }

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

    public int indexOfList(ListField listField, String prefix, int start) {
        return content.indexOf(prefix, start);
    }
}

public int getCurrentPosition() {
    return currentPosition;
}

protected boolean navigationClick(int status, int time) {
    final int index = getCurrentPosition();
    Main.getUiApplication().pushScreen(new Custom_LoadingScreen(1));

    Main.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            if (catsid[index] == 9) {
                if (Config_GlobalFunction.isConnected()) {
                    webpage = new BrowserField();

                    MainScreen aboutus = new Menu_Aboutus();
                    aboutus.add(webpage);
                    Main.getUiApplication().pushScreen(aboutus);

                    webpage.requestContent("http://www.orientaldaily.com.my/index.php?option=com_k2&view=item&id="
                            + newsid[index]
                            + ":&Itemid=223"
                            + Database_Webservice.ht_params);
                } else
                    Config_GlobalFunction.Message(
                            Config_GlobalFunction.nowifi, 1);
            } else {

                Main.getUiApplication().pushScreen(
                        new Main_NewsDetail(newsid[index]));
            }
        }
    }, 1 * 1000, false);
    return true;
}
}

我尝试使用.get,但是当我检查时isFocusable(),它返回给我NullPointerException

我不确定如何获得正确的字段。

4

1 回答 1

1

最后我找到了解决方案。

它由两层字段组成。外部必须设置为列表字段,之后内部必须设置为第一个元素。

while (dy > 0) {
    Field f = listfield;
    if (f.isFocusable()) {
        f.setFocus(); //outer field
        listfield.setSelectedIndex(0); //inner field
        dy--;
    }
}
于 2012-08-07T07:12:48.433 回答