【问题标题】:Calling Between Classes in Blackberry Java在 Blackberry Java 中的类之间调用
【发布时间】:2012-01-27 15:45:47
【问题描述】:

当在屏幕上“单击”位图时,我正在尝试推送一个新屏幕。为此,我从这篇文章中创建了一个类:Blackberry Clickable BitmapField,我在下面发布了它的部分代码:

public class CustomMenuButtonField extends Field{
    Bitmap normal,focused;

    ...

    protected boolean navigationClick(int status, int time)
    {
        // push new screen
        fieldChangeNotify(0);
        return true;
    }

    ...

我想在用户单击位图时推送一个新屏幕。我已经咨询了这个线程: Communicating between classes,但我仍然无法弄清楚调用新屏幕命令的命令。到目前为止我的用户界面是:

public class UserInterface extends UiApplication {
    public static void main(String[] args){
        UserInterface theApp = new UserInterface();
        theApp.enterEventDispatcher();
    }
    public UserInterface() {
        pushScreen(new UserInterfaceScreen());
    }
}

final class UserInterfaceScreen extends MainScreen {
    public UserInterfaceScreen() {
    ...

弹出新屏幕的命令是什么,更重要的是我可以在哪里工作?我知道它可能应该使用 pushScreen() 但这在该类中不被识别。我会创建一个新的final class NewScreenFromClick extends MainScreen吗?如果是这样,我将如何调用它并将其放入 eventDispatcher 中。我一直在浏览黑莓网站,但他们没有太多关于这个问题的示例代码,而且我对 Java 很陌生,所以这对我来说相当混乱。

【问题讨论】:

  • 你要推送的新屏幕类的名称是什么?
  • 例如主屏幕?

标签: java eclipse blackberry


【解决方案1】:
    imageField imageField = new imageField ("",Field.FOCUSABLE,"image.png","image.png", 0x102839);
    add(imageField );
    FieldChangeListener listener = new FieldChangeListener() {
    public void fieldChanged(Field field, int context) {
            if (field == imageField ) {
    home home = new home();//your screen
    UiApplication.getUiApplication().pushScreen(home);

                  }
                }
                         };
imageField .setChangeListener(listener);

//imagefield类在下面给出

package com.pl.button;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

public class imagefield extends Field {

private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;

private Bitmap _currentPicture;
private Bitmap _onPicture; 
private Bitmap _offPicture; 
int color;

public imagefield (String text, long style ,String img, String img_hvr, int color){
    super(style);


    _offPicture = Bitmap.getBitmapResource(img);
    _onPicture = Bitmap.getBitmapResource(img_hvr);

    _font = getFont();
    _label = text;
    _labelHeight = _onPicture.getHeight();  
    _labelWidth = _onPicture.getWidth();

    this.color = color;

    _currentPicture = _offPicture;
}

/**
 * @return The text on the button
 */
String getText(){
    return _label;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredHeight()
 */
public int getPreferredHeight(){
    return _labelHeight;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredWidth()
 */
public int getPreferredWidth(){
    return _labelWidth;
}

/**
 * Field implementation.  Changes the picture when focus is gained.
 * @see net.rim.device.api.ui.Field#onFocus(int)
 */
protected void onFocus(int direction) {
    _currentPicture = _onPicture;
    invalidate();
}

/**
 * Field implementation.  Changes picture back when focus is lost.
 * @see net.rim.device.api.ui.Field#onUnfocus()
 */
protected void onUnfocus() {
    _currentPicture = _offPicture;
    invalidate();
}

/**
 * Field implementation.  
 * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
 */
protected void drawFocus(Graphics graphics, boolean on) {
    // Do nothing
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#layout(int, int)
 */
protected void layout(int width, int height) {
    setExtent(Math.min( width, getPreferredWidth()), 
    Math.min( height, getPreferredHeight()));
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#paint(Graphics)
 */
protected void paint(Graphics graphics){       
    // First draw the background colour and picture
    graphics.setColor(this.color);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

    // Then draw the text
    graphics.setColor(Color.BLACK);
    graphics.setFont(_font);
    graphics.drawText(_label, 4, 2, 
        (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
        getWidth() - 6 );
}

/**
 * Overridden so that the Event Dispatch thread can catch this event
 * instead of having it be caught here..
 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
 */
protected boolean navigationClick(int status, int time){
    fieldChangeNotify(1);
    return true;
}

}

【讨论】:

    【解决方案2】:

    你可以使用:

    UiApplication.getUiApplication().pushScreen(new HomeScreen());
    

    如果您的应用程序没有做任何奇怪的事情,UiApplication.getUiApplication() 将始终为您的程序返回 UiApplication 对象。这是为每个 UiApplication 上下文创建的单例对象。

    你也可以使用:

    UserInterface.getUiApplication().pushScreen(new HomeScreen());
    

    如果 UserInterface 类对您正在处理的类可见,但这会降低您的代码的可重用性。

    我在博客上收集了一些基本示例。看看这个page。从底部开始,逐步向上。

    【讨论】:

    • 感谢 Richard,您提供的命令运行良好。我肯定也会看你的博客页面。您是否碰巧知道 rfsk2010 在下面的帖子中出现的问题(在评论部分进行了解释)?我似乎无法调用代码的按钮部分。
    • 什么都没有。你从模拟器得到什么错误?
    • 应用程序错误 104 未捕获 NullPointerException
    • 好问题,您可能必须尝试插入一些 try{} catch{} 块并找出 NullPointer 的使用位置。
    • 谢谢我试试。会不会是类似于 try{ code being testing here } catch { exception e } 的东西?
    【解决方案3】:

    试试下面我添加了UserInterfaceScreen的代码。在构造 UserInterfaceScreen 时,它会添加一个 CustomMenuButtonField 并设置一个字段更改侦听器。单击按钮时,它将新屏幕推送到堆栈

    package mypackage;
    
    import net.rim.device.api.ui.UiApplication;
    
    public class UserInterface extends UiApplication {
    public static void main(String[] args){
        UserInterface theApp = new UserInterface();
        theApp.enterEventDispatcher();
    }
    public UserInterface() {
        pushScreen(new UserInterfaceScreen());
    }
    }
    

    这是用户界面界面

    package mypackage;
    
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.FieldChangeListener;
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.container.MainScreen;
    
    public class UserInterfaceScreen extends MainScreen {
    
    public UserInterfaceScreen() {
        super();
        //replace null with the image string
        CustomMenuButtonField button = new CustomMenuButtonField(null, null);
        button.setChangeListener(new FieldChangeListener() {
    
            public void fieldChanged(Field field, int context) {
                UiApplication.getUiApplication().pushScreen(new         MyHomeScreen());
    
            }
        });
    }
    }
    

    【讨论】:

    • 感谢代码 UserInterface.getUiApplication().pushScreen(new HomeScreen());确实有效!但是我无法在 super() 下运行按钮功能。它总是会在模拟器中给我一个错误。我几乎复制粘贴了您在我的 super() 调用下提供的示例,并在另一个文件中创建了一个 MyHomeScreen 类。当我注释掉按钮代码时,没有错误。我最终能够通过位图单击navigationClick() 调用该函数。您知道为什么我无法在没有模拟器错误 104 的情况下调用代码的按钮部分吗?
    【解决方案4】:

    在我发布的以下链接中查看答案(alishaik786):

    Clickable Bitmap;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 2014-01-30
      相关资源
      最近更新 更多