Blackberry 6: display menu on long click, run 1 action on short click
Hello fellow BB programmers,
if you look at the native Contacts (Addressbook) application on a Blackberry 6 phone with touch screen - it has a very natural behaviour:
- On a short tap the default action is being executed - viewing the selected addressbook entry
- On a long touch and hold a menu with several actions is displayed:
I'm trying to create an app with a ListField and similar (and intuitive) behaviour myself: run default action on a short tap and display a menu in the middle of screen with several secondary actions on a longer touch.
I've searched a lot and unfortunately only managed to create a test app with exactly opposite behaviour sofar:
I listen for a TouchGesture.HOVER and run editMenu.run(). And for the short tap a menu comes by itself (I haven't found yet, what makes it appear, some method in MainScreen/Screen?). I've tried running onMenu(0) but the menu appears in the top/right corner instead of screen center.
Below is my very simple test code MyList.java, please help me to fix it:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField() {
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.GESTURE) {
TouchGesture gesture = event.getGesture();
if (gesture.getEvent() == TouchGesture.HOVER) {
System.err.println("XXX hover=" + gesture.getHoverCount() + ", index=" + myList.getSelectedIndex());
editMenu.run();
// onMenu(0);
return true;
}
}
return super.touchEvent(event);
}
};
private final MenuItem addMenu = new MenuItem("Add item", 0, 0) {
开发者_StackOverflow社区 public void run() {
Status.show("Adding new item");
}
};
private final MenuItem editMenu = new MenuItem("Edit item", 1, 0) {
public void run() {
Status.show("Editing existing item: " + myList.getSelectedIndex());
}
};
private final MenuItem removeMenu = new MenuItem("Remove item", 2, 0) {
public void run() {
Status.show("Removing existing item: " + myList.getSelectedIndex());
}
};
public MyScreen() {
setTitle("How to display menu on long click?");
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(addMenu);
addMenuItem(editMenu);
addMenuItem(removeMenu);
}
}
Thank you! Alex
The issue described here is related to the new Pop-up menus introduced in the OS 6. Using TouchEvent
is a hack and will not work for all devices (not all OS 6 devices have a touch-screen).
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField() {
protected boolean navigationClick(int status, int time) {
editMenu.run();
return true;
}
};
private final MenuItem addMenu = new MenuItem("Add item", 0, 0) {
public void run() {
Status.show("Adding new item");
}
};
private final MenuItem editMenu = new MenuItem("Edit item", 1, 0) {
public void run() {
Status.show("Editing existing item: " + myList.getSelectedIndex());
}
};
private final MenuItem removeMenu = new MenuItem("Remove item", 2, 0) {
public void run() {
Status.show("Removing existing item: " + myList.getSelectedIndex());
}
};
public MyScreen() {
setTitle("How to display menu on long click?");
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(addMenu);
addMenuItem(editMenu);
addMenuItem(removeMenu);
}
}
Why this works as expected? Adding menu items to the screen implicitly sets a ContextMenuProvider
for the screen (it defines a strategy for displaying a screen's pop-up menu). So the hover works as expected at a screen level - it is the screen who detects the "hover event" and opens the pop-up menu. On the other hand "taps" are handled with the list in navigationClick()
.
精彩评论