How can I click a button field using the blackberry api? I'd like to mimic pressing a button as if the user pressed it.
Suppose you have this code (taken from the BB API doc):
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField buttonField = (ButtonField) field;
System.out.println("Button pressed: " + buttonField.getLabel());
}
};
ButtonField buttonField = new ButtonField("Test Button");
buttonField.setChangeListener(listener);
Then you can programmatically simulate a click by calling the fieldChangeNotify(int context) method of the buttonField. Note that you can distinguish normal/real click from a programmatic one by checking the context in the fieldChanged(Field field, int context). It is the same context you pass in fieldChangeNotify(int context).
Use EventInjector.NavigationEvent like this:
EventInjector.invokeEvent(new EventInjector.NavigationEvent(EventInjector.Navig ationEvent.NAVIGATION_CLICK, 0, 0, 0));
ButtonField buttonField = new ButtonField("Test Button" ,ButtonField.CONSUME_CLICK);
buttonField.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
Dialog.alert("Test Button Clicked");
}
});
Related
I want if anyone click a label then it should trigger a function. Like I want if a user clicks a label then it should go to another page. Following is the code I tried.
Thanks in Advance!!
LabelField joinGroups = new LabelField("Join Groups",LabelField.FOCUSABLE ){
protected void layout(int width, int height) {
super.layout(width, height);
this.setExtent(1000, 44);
}
};
FChangeListener customListenerSurveys = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Surveys Clicked!");
}
};
joinGroups.setFocusListener(customListenerSurveys);
Try Navigation Clack.
LabelField joinGroups = new LabelField("Join Groups",LabelField.FOCUSABLE ){
protected boolean navigationClick(int status, int time){
Dialog.alert("Surveys Clicked!");
return true;
}
};
I want to display button and drop down on a same screen and want to capture it using fieldChnageListener ,i am not able to get how to add these both on the same screen.I have the following code :
ButtonField btnSubmit = new ButtonField("Submit!",
ButtonField.CONSUME_CLICK);
FieldListener listener = new FieldListener();
//assign that listener to the button
btnSubmit.setChangeListener(listener);
add(btnSubmit);
class FieldListener implements FieldChangeListener {
public void fieldChanged(Field f, int context){
//if the submit button is clicked
if (f == btnSubmit){
getCalender();
//if the EditField is empty
if(editField.getText().equals("")){
Dialog.alert("Please enter timezone in the field.");
}else{ // if it is not empty, display the message
Dialog.alert("TimeZone is"+editField.getText()+"!");
timeZone = editField.getText();
}
}
if(f == editField) {
//text changed in the ef-Field (EditField)
}
}
}
how to add a drop down here?
here how can you do this ..
If your class is implementing FieldChangeListner then in its fieldChanged method do like this
public class Demo extends mainScreen implements FieldChangeListener {
public Demo () {
ButtonField btnSubmit = new ButtonField("Submit!",
ButtonField.CONSUME_CLICK);
//assign that listener to the button
btnSubmit.setChangeListener(this);
add(btnSubmit);
}
public void fieldChanged(Field f, int context) {
//if the submit button is clicked
if (f == btnSubmit){
getCalender();
//if the EditField is empty
if(editField.getText().equals("")){
Dialog.alert("Please enter timezone in the field.");
}else{ // if it is not empty, display the message
Dialog.alert("TimeZone is"+editField.getText()+"!");
timeZone = editField.getText();
}
}
if(f == editField) {
//text changed in the ef-Field (EditField)
}
}
}
this should work.
I have written code to attach a fieldchange listener to a button.The issue is that i want this button centered.I know the code to center a button. The issue is to get code to both center the button and include a fieldchange listener to it. Here are some code snippets:
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField register = (ButtonField) field;
UiApplication.getUiApplication().pushScreen(new Register());
}
};
ButtonField register = new ButtonField("Register",ButtonField.CONSUME_CLICK);
register.setChangeListener(listener);
add(register);
How can i center this button?
I put two and two together and came up with this:
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField register = (ButtonField) field;
UiApplication.getUiApplication().pushScreen(new Register());
}
};
ButtonField menu = new ButtonField("Menu", Field.FIELD_HCENTER);
add(menu);
add(new LabelField("New User?",Field.FIELD_HCENTER));
ButtonField register = new ButtonField("Register",ButtonField.CONSUME_CLICK|Field.FIELD_HCENTER);
register.setChangeListener(listener);
add(register);
This piece of code:
ButtonField.CONSUME_CLICK|Field.FIELD_HCENTER
Is what does the trick
After loading the screen If i click on the button directly, the setChangeListener event is not being invoked, rather when the focus is changed to button/hfm, the setChangeListener event is getting called and the desired result is also obtained.
What might be the possible reason and please help me fix this issue...
HorizontalFieldManager hfm = new HorizontalFieldManager();
ButtonField buttonF = new ButtonField(ButtonField.CONSUME_CLICK);
buttonF.setLabel("View Key");
FieldChangeListener listeneronClick = new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
System.out.println("in fieldchange");
ButtonField buttononClick = (ButtonField) field;
buttononClick.setLabel("Hide Key");
}
};
hfm.add(buttonF);
buttonF.setChangeListener(listeneronClick);
Thanks in advance.
Try creating your button like the following
ButtonField button = new ButtonField("Ok", ButtonField.CONSUME_CLICK);
ButtonField.CONSUME_CLICK Indicates to the button consume the click event.
Your FieldChangeListener won't do anything because it creates a new button and does not add it to the screen. I'm guessing you just want to change the text on your existing button so try the following code:
HorizontalFieldManager hfm = new HorizontalFieldManager();
final ButtonField buttonF = new ButtonField(ButtonField.CONSUME_CLICK);
buttonF.setLabel("View Key");
FieldChangeListener listeneronClick = new FieldChangeListener(){
public void fieldChanged(Field field, int context)
{
System.out.println("in fieldchange");
buttonF.setLabel("Hide Key");
}
};
hfm.add(buttonF);
buttonF.setChangeListener(listeneronClick);
try calling hfm.setFocus() before adding hfm to the screen manager.
I'm trying to set the text on a button when it is clicked. I'm initialising a BigVector which will update the text of the button with its value. I'm using a counter value to determine wihcih BigVector value should be selected. The problem is, the below code is expecting the counter value to be final.
A better methodology for updating the text on a Field when it is clicked is most welcome.
Here is my code -
final BigVector bigStringVectorA = new BigVector();
bigStringVectorA.addElement("A Test answer 1");
bigStringVectorA.addElement("A Test answer 2");
bigStringVectorA.addElement("A Test answer 3");
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
}
});
Thanks
You can make the counter an instance variable, either in the outer class or in the anonymous FieldChangeListener:
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
private int counter = 0;
public void fieldChanged(Field field, int context) {
counter++;
if (counter > bigStringVectorA.size()) {
counter = 0;
}
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
}
});
You can try to call the invalidate() method of that field and that should force the redraw of that button.
aAnswerOptionButton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
aAnswerOptionButton.setText((String)bigStringVectorA.elementAt(counter));
aAnswerOptionButton.invalidate();
}
});