blackberry ButtonField application sample
-
02-03-2021 - |
Question
I am very new in black berry application.
now trying to create a calculator in blackberry using eclipse:
so i have added a button (ButtonField
), my first target is when press this button i want display
"hi.. now you can try with text field."
here i put my code, please go through it.
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");
}
}
hello whats the wrong on this. ? pls help.. it will quite simple for you, but me not now ?
Solution
Check this out.
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);
}
}
}
OTHER TIPS
In field-changed listener, replace this code
public void fieldChanged(Field field, int context) {
System.out.println("hi.. now you can try with text field");
}
with
public void fieldChanged(Field field, int context) {
if(field == mySubmitButton) {
System.out.println("hi.. now you can try with text field");
}
}
Don't write just what you want to do. First check if it is a ButtonField then write code for it.
Try this:
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");
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow