2011-11-17 144 views
2

我是非常新的黑莓應用程序。黑莓ButtonField應用程序示例

現在試圖建立在黑莓使用eclipse計算器:

,所以我增加了一個按鈕(ButtonField),我的第一個目標是當按下此按鈕,我想顯示

喜..現在你可以嘗試使用文本字段

這裏我把我的代碼,請通過它。

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"); 

    } 
} 

招呼什麼錯了這一點。 ?請幫助.. 它會很簡單,但我現在不是?

回答

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); 
    } 
} 
} 
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,然後爲其編寫代碼。

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"); 

     } 
    });