在Blackberry Java中的类之间调用

时间:2012-01-27 15:45:48

标签: java eclipse blackberry

我想在屏幕上“点击”位图时按下新屏幕。为此,我在这篇文章中创建了一个类: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,但我仍然无法弄清楚调用新屏幕命令的命令。我到目前为止的UserInterface是:

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(),但在该类中无法识别。我会创建一个新的最终类NewScreenFromClick扩展MainScreen 吗?如果是这样,我将如何调用它并将其放入eventDispatcher中。我一直在浏览黑莓网站,但他们在这个问题上没有太多的示例代码,我对Java很新,所以这对我来说相当混乱。

4 个答案:

答案 0 :(得分:2)

您可以使用:

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

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

你也可以使用:

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

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

我在博客上收集了一些基本样本。看看这个page。从底部开始,继续前进。

答案 1 :(得分:2)

    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 :(得分:1)

尝试使用以下代码添加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());
}
}

这是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());

        }
    });
}
}

答案 3 :(得分:0)

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

Clickable Bitmap;

相关问题