Blackberry:检测长按 ListField 并显示菜单

发布于 2024-11-05 14:51:55 字数 1503 浏览 0 评论 0原文

如何检测 ListField 组件上的长按?

您是否重写其 navigationClick(int status, int time) 并摸索其 time 参数(如何?),或者是否有一些内置方法来检测长时间点击?

更重要的是 - 一旦检测到这样的点击,如何显示菜单(屏幕中间的菜单)?

背景是,通过短暂的点击,我想让用户编辑所选的项目。长按时我想在屏幕中间显示一个菜单以提供辅助任务:删除项目、更改项目显示顺序等。

下面是我当前的测试代码 - src\mypackage\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 navigationClick(int status, int time) {
            System.err.println("XXX status=" + status + ", index=" + getSelectedIndex());
            return true;
        }
    };

    public MyScreen() {
        setTitle("How to detect long click?");
        myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", }); 
        add(myList);
    }
}

谢谢 亚历克斯

How do you detect a long click on a ListField component?

Do you override its navigationClick(int status, int time) and fumble with its time argument (how?) or is there some builtin method for detecting long clicks?

And more importantly - how do you display the menu (the one in the middle of the screen) once you detected such a click?

The background is that on short clicks I'd like to let the user edit the selected item. And on long clicks I'd like to display a menu in the middle of the screen to offer secondary tasks: delete item, change item display order, etc.

Below is my current test code - src\mypackage\MyList.java:

enter image description here

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 navigationClick(int status, int time) {
            System.err.println("XXX status=" + status + ", index=" + getSelectedIndex());
            return true;
        }
    };

    public MyScreen() {
        setTitle("How to detect long click?");
        myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", }); 
        add(myList);
    }
}

Thank you
Alex

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

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

发布评论

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

评论(1

榆西 2024-11-12 14:51:55

您可以覆盖 touchEvent 字段的方法。然后做这样的事情:

ObjectListField myList = new ObjectListField() {
        long touchedAt = -1;
        long HOLD_TIME = 2000; // 2 seconds or whatever you define the hold time to be
        protected boolean touchEvent(TouchEvent message) {
            if(message.getEvent() == TouchEvent.DOWN) {
               touchedAt = System.currentTimeMillis();
            } else if(message.getEvent() == TouchEvent.UP) {
               if(System.currentTimeMillis() - touchedAt < HOLD_TIME)
                  touchedAt = -1; // reset
               else
                  //write logic you need for touch and hold
            }
            return true;
        }
    };

请注意,这是一个粗略的实现,只是为了给您一个想法。我这里只使用了时间坐标。您的实现可能需要考虑用户触摸屏幕位置的 X 和 Y 坐标,因为如果他移动手指,那么就不是触摸并按住

You can override the touchEvent method of your Field. Then do something like this:

ObjectListField myList = new ObjectListField() {
        long touchedAt = -1;
        long HOLD_TIME = 2000; // 2 seconds or whatever you define the hold time to be
        protected boolean touchEvent(TouchEvent message) {
            if(message.getEvent() == TouchEvent.DOWN) {
               touchedAt = System.currentTimeMillis();
            } else if(message.getEvent() == TouchEvent.UP) {
               if(System.currentTimeMillis() - touchedAt < HOLD_TIME)
                  touchedAt = -1; // reset
               else
                  //write logic you need for touch and hold
            }
            return true;
        }
    };

Please note that this is a rough implementation just to give you an idea. I only used the time coordinate here. Your implementation will probably need to factor in the X and Y coordinates of where the user touched the screen, because if he moved his finger, then that wouldn't be a touch and hold.

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