黑莓ButtonField应用程序示例

时间:2011-11-17 08:31:52

标签: blackberry buttonfield changelistener

我是黑莓应用的新手。

现在尝试使用eclipse在黑莓中创建一个计算器:

所以我添加了一个按钮(ButtonField),我的第一个目标就是按下这个按钮我要显​​示

" hi ..现在您可以尝试使用文字字段。"

这里我把我的代码,请通过它。

Launcher.java

public class Launcher extends UiApplication {
    public static void main(String[] args) {
        Launcher theApp = new Launcher();
        theApp.enterEventDispatcher();
   }
   private Launcher()
   {
       this.pushScreen(new MainScrn());
   }

}

MainScrn .java

public class MainScrn extends MainScreen implements FieldChangeListener {
    public MainScrn() {
        LabelField lf_hello = new LabelField();
        lf_hello.setText("Hello, World!");
        lf_hello.setBackground(BackgroundFactory.createSolidBackground(124));
        ButtonField mySubmitButton = new ButtonField("clickMe");
        mySubmitButton.setChangeListener(this);
        this.add(lf_hello);
        this.add(mySubmitButton);
    }

    public void fieldChanged(Field field, int context) {
        System.out.println("hi.. now you can try with text field");

    }
}

你好,这是错的。 ?请帮忙.. 这对你来说很简单,但我现在不是吗?

3 个答案:

答案 0 :(得分:2)

在字段更改的侦听器中,替换此代码

 public void fieldChanged(Field field, int context) {
        System.out.println("hi.. now you can try with text field");
 }

public void fieldChanged(Field field, int context) {
    if(field == mySubmitButton) {
        System.out.println("hi.. now you can try with text field");
    }      
}

不要只写你想做的事。首先检查它是否是ButtonField,然后为其编写代码。

答案 1 :(得分:2)

试试这个:

buttons.setChangeListener(new FieldChangeListener()
    {

        public void fieldChanged(Field field, int context)
        {
            System.out.println("hi.. now you can try with text field");
            Dialog.alert("hi.. now you can try with text field");

        }
    });

答案 2 :(得分:1)

检查一下。

public final class MyScreen extends MainScreen implements FieldChangeListener
{
/**
 * Creates a new MyScreen object
 */

LabelField lbl = new LabelField("hi.. now you can try with text field.");
ButtonField bf = new ButtonField("Click Me",ButtonField.CONSUME_CLICK);
public MyScreen()
{        
    // Set the displayed title of the screen       
    setTitle("MyTitle");
    bf.setChangeListener(this);
    add(bf);

}

public void fieldChanged(Field field, int context) {
    // TODO Auto-generated method stub
    if(field == bf)
    {
        add(lbl);
    }
}
}