【问题标题】:Label/Name for Bitmap Button Field位图按钮字段的标签/名称
【发布时间】:2012-04-01 11:58:10
【问题描述】:

目前我的应用程序中有 4 个位图按钮。我希望每个按钮都有类似标签/名称的东西,这样每当有人关注特定按钮时,名称就会出现在屏幕上的某处。它可以在顶部、按钮下方或任何地方都可以。

我该怎么做?我尝试搜索位图按钮字段标签,但没有发现任何真正有用的信息。

【问题讨论】:

    标签: blackberry java-me


    【解决方案1】:

    您可以在按钮字段下方放置一个 CustomLabelField。覆盖您的 ButtonFields onFocus(int direction)onUnfocus() 方法。在它们内部调用 CustomLabelField 的 setLabel(String label) 方法

    class CustomLabelField extends Field {
        String label;
        public void setLabel(String label){
            this.label = label;
            invalidate();
        }
        protected void layout(int arg0, int arg1) {
            setExtent(Display.getWidth, getFont().getHeight());
        }
    
        protected void paint(Graphics graphics) {
            graphics.setColor(Color.Black);
            graphics.drawText(label, 0, 0);
        }
    }
    

    编辑(在 cmets 之后)

    您可以使用此自定义按钮并在其中添加其他功能。我没有尝试这是否有效,但应该可以。

    import net.rim.device.api.ui.component.BitmapField;
    import net.rim.device.api.ui.container.MainScreen;
    
    public class CustomButtonField extends BitmapField{
        private String label = "";
        private MainScreen yourScreen;
    
        public CustomButtonField(String label, MainScreen yourScreen) {
            super();
            this.label = label;
            this.yourScreen = yourScreen;
        }
    
        protected void onFocus(int direction) {
            yourScreen.setTitle(label);
            super.onFocus(direction);
        }
    
        protected void onUnfocus() {
            yourScreen.setTitle("");
            super.onUnfocus();
        }
    }
    

    【讨论】:

    • 如果我希望标签覆盖应用程序标题栏,可以吗?不过帮了大忙,非常感谢~
    • 由于每个按钮都有自己的名称/标签,名称分布如何?
    • 好吧,我在回答中解释了这一点。您需要添加一些我的 CustomButtonField 具有的额外属性,即标签和 yourScreen。之后在 onFocus 和 onUnfocus 方法中,您需要将您的屏幕标题设置为这个标签。
    • 如果我使用位图按钮字段,同样的方法是否适用?
    • 是的,您需要覆盖这些方法并添加另一个属性,您将在 BitmapButtonField 上对其进行操作。
    猜你喜欢
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多