How can I click a button field using the blackberry api? I'd li开发者_如何转开发ke 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");
}
});
精彩评论