黑莓上的字幕文本

发布于 2024-08-10 03:22:32 字数 34 浏览 5 评论 0原文

我想在 BlackBerry 应用程序中选取一些文本。

I want to marquee some text in a BlackBerry app.

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

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

发布评论

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

评论(1

孤单情人 2024-08-17 03:22:32

这里是。只需创建一个名为 MarqueeLabel 的类并复制粘贴此代码,然后您就可以使用此类来显示选取框文本:

package mypackage;

import java.util.Timer;

import java.util.TimerTask;

import net.rim.device.api.system.Display;

import net.rim.device.api.ui.DrawStyle;

import net.rim.device.api.ui.Font;

import net.rim.device.api.ui.Graphics;

import net.rim.device.api.ui.component.LabelField;

class MarqueLabel extends LabelField {

    int currentChar = 0;

    String currentText = null;

    Font ourFont;

    private Timer _scrollTimer;

    private TimerTask _scrollTimerTask;

    public MarqueLabel(String text, long style) {

        super(text, style);     

    }

    public void paint(Graphics graphics) {

        currentText = this.getText();

        if (currentChar < currentText.length()) {

            currentText = currentText.substring(currentChar);

        }

        graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, Display.getWidth());

        super.paint(graphics);

    }

    public void layout(int width, int height) {

        ourFont = this.getFont();

        setExtent(500, ourFont.getHeight());

    }

    protected void onDisplay() {

        startScroll();

    }

    protected void onUnfocus() {

        startScroll();

    }

    private void startScroll() {

        // Start scrolling

        final String fullText = this.getText();

        if (_scrollTimer == null) {

            _scrollTimer = new Timer();

            _scrollTimerTask = new TimerTask() {

                public void run() {

                    currentChar = currentChar + 2;

                    if (currentChar > fullText.length()) {

                        currentChar = 0;

                    }

                    invalidate();

                }

            };

            _scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 450);

        }

    }

    protected void onFocus(int direction) {

        if (_scrollTimer != null) {

            _scrollTimerTask.cancel();

            _scrollTimer.cancel();

            _scrollTimer = null;

            _scrollTimerTask = null;

        }

    }

    protected boolean navigationMovement(int dx, int dy, 

        int status, int time) {

        currentText = this.getText();

        int oldCurrentChar = currentChar;

        if (Math.abs(dx) > Math.abs(dy)) {

            if (dx > 0) {

                currentChar = Math.min(currentText.length() - 1,

                        currentChar + 1);

            } else if (dx < 0) {

                currentChar = Math.max(0, currentChar - 1);

            }

            if (oldCurrentChar != currentChar) {

                this.invalidate();

            }

            return true;

        } else {

            return super.navigationMovement(dx, dy, status, time);

        }

    }

    }

Here it is. Just make a class with name MarqueeLabel and copy-paste this code then you can use this class to display a marquee text:

package mypackage;

import java.util.Timer;

import java.util.TimerTask;

import net.rim.device.api.system.Display;

import net.rim.device.api.ui.DrawStyle;

import net.rim.device.api.ui.Font;

import net.rim.device.api.ui.Graphics;

import net.rim.device.api.ui.component.LabelField;

class MarqueLabel extends LabelField {

    int currentChar = 0;

    String currentText = null;

    Font ourFont;

    private Timer _scrollTimer;

    private TimerTask _scrollTimerTask;

    public MarqueLabel(String text, long style) {

        super(text, style);     

    }

    public void paint(Graphics graphics) {

        currentText = this.getText();

        if (currentChar < currentText.length()) {

            currentText = currentText.substring(currentChar);

        }

        graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, Display.getWidth());

        super.paint(graphics);

    }

    public void layout(int width, int height) {

        ourFont = this.getFont();

        setExtent(500, ourFont.getHeight());

    }

    protected void onDisplay() {

        startScroll();

    }

    protected void onUnfocus() {

        startScroll();

    }

    private void startScroll() {

        // Start scrolling

        final String fullText = this.getText();

        if (_scrollTimer == null) {

            _scrollTimer = new Timer();

            _scrollTimerTask = new TimerTask() {

                public void run() {

                    currentChar = currentChar + 2;

                    if (currentChar > fullText.length()) {

                        currentChar = 0;

                    }

                    invalidate();

                }

            };

            _scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 450);

        }

    }

    protected void onFocus(int direction) {

        if (_scrollTimer != null) {

            _scrollTimerTask.cancel();

            _scrollTimer.cancel();

            _scrollTimer = null;

            _scrollTimerTask = null;

        }

    }

    protected boolean navigationMovement(int dx, int dy, 

        int status, int time) {

        currentText = this.getText();

        int oldCurrentChar = currentChar;

        if (Math.abs(dx) > Math.abs(dy)) {

            if (dx > 0) {

                currentChar = Math.min(currentText.length() - 1,

                        currentChar + 1);

            } else if (dx < 0) {

                currentChar = Math.max(0, currentChar - 1);

            }

            if (oldCurrentChar != currentChar) {

                this.invalidate();

            }

            return true;

        } else {

            return super.navigationMovement(dx, dy, status, time);

        }

    }

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