Blackberry 6:长按时显示菜单,短按时运行 1 个操作

发布于 2024-11-06 06:26:15 字数 2781 浏览 3 评论 0原文

各位 BB 程序员大家好,

如果您查看带有触摸屏的 Blackberry 6 手机上的本机联系人(地址簿)应用程序,它具有非常自然的行为:

  1. 短按一下,就会执行默认操作 - 查看选定的地址簿
  2. 条目长按并按住会显示包含多个操作的菜单:

Addressbook

我正在尝试创建一个带有 ListField 和类似内容的应用程序我自己的(直观的)行为:短按即可运行默认操作,并在屏幕中间显示一个菜单,长时间触摸时会显示多个辅助操作

我已经搜索了很多,不幸的是到目前为止只成功创建了一个具有完全相反行为的测试应用程序:

我监听 TouchGesture.HOVER 并运行 editMenu.run()。对于短按,菜单本身会出现(我还没有找到,是什么让它出现,主屏幕/屏幕中的某种方法?)。我尝试运行 onMenu(0) 但菜单出现在右上角/右上角而不是屏幕中心。

MyList app

下面是我非常简单的测试代码MyList.java,请帮我修复它:

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) {  
        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);
    }
}

谢谢你! 亚历克斯

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:

  1. On a short tap the default action is being executed - viewing the selected addressbook entry
  2. On a long touch and hold a menu with several actions is displayed:

Addressbook

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.

MyList app

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) {  
        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2024-11-13 06:26:15

此处描述的问题与新的弹出菜单有关在 OS 6 中引入。使用 TouchEvent 是一种黑客行为,不适用于所有设备(并非所有 OS 6 设备都有触摸屏)。

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

为什么这会按预期工作?向屏幕添加菜单项会隐式地为屏幕设置一个 ContextMenuProvider(它定义了显示屏幕弹出菜单的策略)。因此,悬停在屏幕级别按预期工作 - 屏幕检测“悬停事件”并打开弹出菜单。另一方面,“点击”是通过 navigationClick() 中的列表进行处理的。

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文