BlackBerry - 以编程方式显示打字模式指示器

时间:2010-03-01 11:15:37

标签: user-interface blackberry input keyboard

你知道某种方法可以在屏幕上显示不同的typing indicators吗? 我知道我可以简单地绘制位图,但我想为任何RIM OS版本普遍做到这一点 此外,4.2.1中有一个setMode(int)函数,但在4.3中它已被弃用...

任何建议都会有所帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

由于没有其他选择,我使用provided images制作了一个示例:

alt text http://img42.imageshack.us/img42/6692/typeindicator.jpg alt text http://img3.imageshack.us/img3/5259/inputind.jpg

自定义标题字段类代码:

class TITitleField extends Field implements DrawStyle {
    static final boolean mIsDimTheme = Integer.parseInt(DeviceInfo
            .getDeviceName().substring(0, 4)) < 8900;
    static final Bitmap ALT = Bitmap.getBitmapResource(mIsDimTheme ? 
            "typ_ind_alt_mode_Gen_Zen_328560_11.jpg" : 
            "typ_ind_alt_mode_Precsn_Zen_392908_11.jpg");
    static final Bitmap MULTITAP = Bitmap.getBitmapResource(mIsDimTheme ?
    "typ_ind_mltap_mode_Gen_Zen_328975_11.jpg" : 
    "typ_ind_mutlitap_mode_Precsn_Zen_452907_11.jpg");
    static final Bitmap NUMLOCK = Bitmap
        .getBitmapResource(mIsDimTheme ? 
            "typ_ind_num_lock_Gen_Zen_328568_11.jpg" : 
            "typ_ind_num_lock_Precsn_Zen_392925_11.jpg");
    static final Bitmap SHIFT = Bitmap.getBitmapResource(mIsDimTheme ? 
            "typ_ind_shift_mode_Gen_Zen_328574_11.jpg" : 
            "typ_ind_shift_mode_Precsn_Zen_392931_11.jpg");

    public static final int MODE_NONE = 0;
    public static final int MODE_ALT = 1;
    public static final int MODE_MULTITAP = 2;
    public static final int MODE_NUMLOCK = 3;
    public static final int MODE_SHIFT = 4;

    public void setTypingIndicatorMode(int mode) {
        mMode = mode;
        updateLayout();
    }

    public int getTypingIndicatorMode()
    {
        return mMode;       
    }

    int mWidth = 0;
    int mMode = 0;
    String mTitle = "";
    XYRect mIndicatorDestRect = new XYRect();

    public TITitleField() {
        this("");
    }

    public TITitleField(String title) {
        mTitle = title;
    }

    protected void paint(Graphics graphics) {
        graphics.drawText(mTitle, 0, 0, LEFT | ELLIPSIS, mWidth);
        if (0 != mMode) {
            graphics.drawBitmap(mIndicatorDestRect,getIndicator(mMode),0,0);
        }
    }

    private static Bitmap getIndicator(int mode) {
        Bitmap result = null;
        switch (mode) {
        case MODE_ALT:
            result = ALT;
            break;
        case MODE_MULTITAP:
            result = MULTITAP;
            break;
        case MODE_NUMLOCK:
            result = NUMLOCK;
            break;
        case MODE_SHIFT:
            result = SHIFT;
            break;
        case MODE_NONE:
            break;
        default:
            break;
        }
        return result;
    }

    protected void layout(int width, int height) {
        mWidth = width;
        if (0 != mMode) {
            Bitmap indicator = getIndicator(mMode);
            mIndicatorDestRect.width = indicator.getWidth();
            mIndicatorDestRect.height = indicator.getHeight();
            mIndicatorDestRect.y = 0;
            mIndicatorDestRect.x = mWidth - mIndicatorDestRect.width;
        }
        setExtent(width, getPreferredHeight());
    }

    public int getPreferredHeight() {
        int height = getFont().getHeight() + 4;
        if (0 != mMode) {
            int indicatorHeight = getIndicator(mMode).getHeight();
            height = Math.max(height, indicatorHeight);
        }
        return height;
    }
}

使用代码示例:

class Scr extends MainScreen {
    static final TITitleField mTitle = new TITitleField("Start");

    public Scr() {
        this.setTitle(mTitle);
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);

        int typingIndicatorMode = mTitle.getTypingIndicatorMode();

        if(typingIndicatorMode != mTitle.MODE_NONE)
        menu.add(new MenuItem("None Mode", 0, 0) {
            public void run() {
                mTitle.setTypingIndicatorMode(mTitle.MODE_NONE);
            }
        });

        if(typingIndicatorMode != mTitle.MODE_ALT)
        menu.add(new MenuItem("Alt Mode", 0, 0) {
            public void run() {
                mTitle.setTypingIndicatorMode(mTitle.MODE_ALT);
            }
        });

        if(typingIndicatorMode != mTitle.MODE_MULTITAP)
        menu.add(new MenuItem("Multitap Mode", 0, 0) {
            public void run() {
                mTitle.setTypingIndicatorMode(mTitle.MODE_MULTITAP);
            }
        });

        if(typingIndicatorMode != mTitle.MODE_NUMLOCK)
        menu.add(new MenuItem("NumLock Mode", 0, 0) {
            public void run() {
                mTitle.setTypingIndicatorMode(mTitle.MODE_NUMLOCK);
            }
        });

        if(typingIndicatorMode != mTitle.MODE_SHIFT)
        menu.add(new MenuItem("Shift Mode", 0, 0) {
            public void run() {
                mTitle.setTypingIndicatorMode(mTitle.MODE_SHIFT);
            }
        });
    }
}