0

我想手动显示一个字段。

public class Main_NewsDetail extends MainScreen {
private Custom_FontField slider;
private boolean a;

public Main_NewsDetail() {
    super(USE_ALL_WIDTH);

    slider = new Custom_FontField(
            Bitmap.getBitmapResource("slider_thumb_normal.png"),
            Bitmap.getBitmapResource("slider_progress_normal.png"),
            Bitmap.getBitmapResource("slider_base_normal.png"),
            Bitmap.getBitmapResource("slider_thumb_focused.png"),
            Bitmap.getBitmapResource("slider_progress_focused.png"),
            Bitmap.getBitmapResource("slider_base_focused.png"),
            Bitmap.getBitmapResource("slider_thumb_pressed.png"),
            Bitmap.getBitmapResource("slider_progress_pressed.png"),
            Bitmap.getBitmapResource("slider_base_pressed.png"), 35, 10, 5,
            5, FOCUSABLE);
    if(a)
    add(slider);


}

public class Custom_NewsDetailBottom extends Manager implements
        FieldChangeListener {


    Custom_NewsDetailBottom() {
        super(Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL
                | Manager.NO_HORIZONTAL_SCROLL);
        Background background = BackgroundFactory
                .createBitmapBackground(bg);
        setBackground(background);
        fontbtn = new Custom_ButtonField(font, fontactive, fontactive) {
            protected boolean navigationClick(int status, int time) {
                a = !a;   <-- here is to control field display
                return true;
            }
        };
        fontbtn.setChangeListener(this);
        add(fontbtn);


    }

    protected void sublayout(int width, int height) {
        Field field = getField(0);
        layoutChild(field, font.getWidth(), font.getHeight());
        setPositionChild(field, getGap(), 5);



        width = Math.min(width, getPreferredWidth());
        height = Math.min(height, getPreferredHeight());
        setExtent(width, height);
    }

    public int getPreferredHeight() {
        return 70;
    }

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

    protected void paint(Graphics graphics) {
        int rectHeight = getPreferredHeight();
        int rectWidth = getPreferredWidth();
        graphics.drawRect(0, 0, rectWidth, rectHeight);
        super.paint(graphics);
    }

    private int getGap() {
        return ((getPreferredWidth() / 4) - font.getWidth()) / 2;
    }

    public void fieldChanged(Field field, int context) {
        if (field == sharebtn) {

        } else if (field == commentbtn) {
            Main.getUiApplication().pushScreen(new Main_Comments());
        } else if (field == otherbtn) {
            Main.getUiApplication().pushScreen(new Menu_Others());
        }
    }

    public boolean keyDown(int keycode, int status) {
        if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
            delete(slider);
            return true;
        }
        return super.keyDown(keycode, status);
    }
}
}

以上是显示屏幕的代码。在 fontbtn 中,单击时将更改变量true / false。但是,它不能立即更新以显示该字段slider

slider类似于 seekbar in Android。在android中,当click然后可以setvisibility但不是blackberry RIM,那么如何控制呢?

4

2 回答 2

0

尝试调用 Manager 的invalidate方法来强制重新绘制托管区域:

Marks this entire manager as requiring repainting.
Invoke this method to signal that this manager's entire region requires repainting. 
于 2012-07-09T10:10:50.173 回答
0

首先,如果可以避免的话,我建议不要使用具有这样名称的变量:

private boolean a;

尝试给它起一个更具描述性的名称,因为这将有助于我们更好地理解您的代码。

接下来,看起来您a在构造函数中测试变量之前它有机会更改Main_NewsDetail。所以这行不通。也许在Main_NewsDetail课堂上试试这个:

/** separate boolean used because Field.isVisible() doesn't seem totally robust */
private boolean isSliderVisible = false;

private void setSliderVisible(boolean isVisible) {
   if (isVisible != isSliderVisible) {
      if (isVisible) {
          add(slider);
      } else {
          delete(slider);
          // but, we still retain the "slider" member variable, so it can be
          // added again later
      }
      isSliderVisible = isVisible;

      // I'm not actually sure that this is needed.  I include it because I can't run this code right now!
      invalidate();
   }  
}

然后,在您的按钮单击处理程序中:

fontbtn = new Custom_ButtonField(font, fontactive, fontactive) {
     protected boolean navigationClick(int status, int time) {
         setSliderVisible(!isSliderVisible);
         return true;
     }
};

现在,这段代码可以工作了,因为你Main_NewsDetail只有一个Field,滑块。如果实际上有多个Field对象(您可能会有),那么您可能需要更复杂的逻辑。您可能希望每次都在同一位置显示滑块。为此,您可以index在所有 s 字段的列表中记录滑块的Main_NewsDetail(例如,滑块是第 1 字段、第 2 字段、第 5 字段吗?)。然后,而不是调用add(slider),你会这样做:

private void setSliderVisible(boolean isVisible) {
   if (isVisible != isSliderVisible) {
      if (isVisible) {
         insert(slider, sliderIndex);

您可能需要创建Main_NewsDetail一个子类Manager并实现该sublayout()方法。这样,您可以确保如果isSliderVisible,您始终将滑块布置在相同的位置。

于 2012-07-09T10:58:18.260 回答