⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultinputmethodhandler.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	int n = 0;	// get the current input mode	for (; n < allowedModesNum; n++) {	    if (allowedModes[n] == inputMode) {		break;	    }	}        // advance the input mode        n++;        if (n >= allowedModesNum) {            n = 0;        }	return allowedModes[n];    }    /**     * Set the input mode.     *     * @param mode The new input mode to use     * @return boolean True, if the mode was valid     */    protected boolean setInputMode(int mode) {	switch (mode) {	case IM_ROMAN_CAPS:	    keyMap = upperRomanKeyMap;	    break;	case IM_ROMAN_SMALL:	    keyMap = lowerRomanKeyMap;	    break;	case IM_NUMERIC:	    keyMap = (constraint == TextField.PHONENUMBER) ?                phoneNumericKeyMap : numericKeyMap;	    break;	case IM_SYMBOL:            st.invokeSYM();	    break;	case IM_NONE:	    break;        default:            return false;	}        imc.setInputMode(inputMode = mode);        return true;    }    /**     * Establish the constraints of this handler.     */    protected void setConstraint() {        if (constraint == TextField.NUMERIC ||            constraint == TextField.PHONENUMBER) {            defaultMode = IM_NUMERIC;            allowedModesNum = 1;            allowedModes[0] = IM_NUMERIC;        } else if (constraint == TextField.EMAILADDR ||            constraint == TextField.URL) {            defaultMode = IM_ROMAN_SMALL;            allowedModesNum = 4;            allowedModes[0] = IM_ROMAN_SMALL;            allowedModes[1] = IM_ROMAN_CAPS;            allowedModes[2] = IM_NUMERIC;            allowedModes[3] = IM_SYMBOL;        } else {            String dMode = null; // default mode            String aMode[] = null;  // allowed modes            if (constraint == TextField.ANY) {                Object obj = imc.getClientObject();                if (obj instanceof TextBox) {                    dMode = InputMode.getMode((TextBox)obj);                    aMode = InputMode.getAllowedModes((TextBox)obj);                } else if (obj instanceof TextField) {                    dMode = InputMode.getMode((TextField)obj);                    aMode = InputMode.getAllowedModes((TextField)obj);                }            }                        allowedModesNum = 0;            if (dMode == null && aMode == null) {                for (int i = 0; i < inputModeConvTable.length; i++) {                    allowedModes[allowedModesNum++] =                         ((Integer)inputModeConvTable[i][1]).intValue();                }                defaultMode = allowedModes[0];            } else {                if (dMode == null && aMode != null) {                    dMode = aMode[0];                }                if (dMode != null && aMode == null) {                    aMode = new String[supportedInputModes.length];                    System.arraycopy(supportedInputModes, 0,                                      aMode, 0, supportedInputModes.length);                }                                int i, j;                for (i = 0; i < inputModeConvTable.length; i++) {                    if (((String)inputModeConvTable[i][0]).                        equals(dMode)) {                        defaultMode =                             ((Integer)inputModeConvTable[i][1]).intValue();                        break;                    }                }                for (j = 0; j < inputModeConvTable.length; j++) {                    for (i = 0; i < aMode.length; i++) {                        if (((String)inputModeConvTable[j][0]).                            equals(aMode[i])) {                            allowedModes[allowedModesNum++] =                                 ((Integer)inputModeConvTable[j][1]).intValue();                        }                    }                }                if (defaultMode == IM_NONE) {                    defaultMode = allowedModes[0];                } else {                    if (defaultMode != allowedModes[0]) {                        for (i = 0; i < allowedModesNum; i++) {                            if (defaultMode == allowedModes[i]) {                                break;                            }                        }                        int t = allowedModes[0];                        allowedModes[i] = t;                        allowedModes[0] = defaultMode;                    }                }            }        }    }    /**     * A special Canvas to display a symbol table.     */    protected class SymbolTable extends Canvas {        /** The margin size */        private final int MARGIN = 1;        /** The margin size */        private final int DMARGIN = 2;        /** Cell size */        private int cc;        /** Height margin */        private int hmargin;        /** Width margin */        private int wmargin;        /** Margin for the cursor */        private int margin;        /** Window x position */        private int wx;        /** Window y position */        private int wy;        /** Window width */        private int ww;        /** Window height */        private int wh;        /** Number of columns */        private int cols;        /** Number of rows */        private int rows;        /** Current cursor position */        private int pos;        /** New cursor position */        private int newpos;        /** Font */        private Font font;        /** Flag */        private boolean firstTime = false;        /** The current Display object */        private Display currentDisplay;        /** The previous Displayable */        private Displayable previousScreen;        /**         * Initialize the symbol table         */        void init() {            if (symbolTableChars.length <= 25) {                rows = cols = 5;            } else {                rows = 6;                cols = 6;            }            int w = getWidth() / cols;            int h = getHeight() / rows;            cc = (w > h) ? h : w;            int cw = 0, ch = 0;            int[] fs = {Font.SIZE_LARGE, Font.SIZE_MEDIUM, Font.SIZE_SMALL};            for (int i = 0; i < fs.length; i++) {                font = Font.getFont(Font.FACE_SYSTEM,                                    Font.STYLE_BOLD,                                    fs[i]);                cw = font.charWidth('M');                ch = font.getHeight();                if (cw <= cc && ch <= cc) {                    break;                }            }            ww = cols * cc;            wh = rows * cc;            wx = (getWidth() - ww) / 2;            wy = getHeight() - wh;            hmargin = (cc - ch) / 2;            wmargin = cc / 2;            margin = hmargin + 2;        }        /**         * Invoke this symbol table.         */        public void invokeSYM() {            if (font == null) {                init();            }	    currentDisplay = imc.getDisplay();            previousScreen = currentDisplay.getCurrent();            currentDisplay.setCurrent(this);            hasSymbolTable = true;        }        /**         * Notify this symbol table its being shown on the screen.	 * Overrides Canvas.showNotify.         */        protected void showNotify() {            pos = newpos = defaultSymbolCursorPos;        }        /**         * Notify this symbol table its being hidden.	 * Overrides Canvas.hideNotify.         */        protected void hideNotify() {            firstTime = false;        }        /**         * Paint this symbol table.	 * Overrides Canvas.paint.         *         * @param g The Graphics object to paint to         */        protected void paint(Graphics g) {            if (!firstTime) {                paintPanel(g);                firstTime = true;            }            showCursor(g, pos, false);            showCursor(g, pos = newpos, true);        }        /**         * Paint the symbol table panel         *         * @param g The Graphics object to paint to         */        void paintPanel(Graphics g) {            g.setFont(font);            g.setGrayScale(255);            g.setClip(0, 0, getWidth(), getHeight());            g.fillRect(0, 0, getWidth(), getHeight());            g.setGrayScale(0);            g.drawRect(wx+1, wy+1, ww-2, wh-2);            for (int r = 0; r < rows; r++) {                for (int c = 0; c < cols; c++) {                    int i = r * cols + c;                    if (i == symbolTableChars.length) {                        break;                    }                    drawChar(g, symbolTableChars[i], r, c, false);                }            }        }        /**         * Draw a character         *         * @param g The Graphics object to paint to         * @param c The character to draw         * @param row The row the character is in         * @param col The column the character is in         * @param reverse A flag to draw the character in inverse         */        void drawChar(Graphics g, char c, int row, int col, boolean reverse) {            int h = font.charWidth(c);            int y = wy + row * cc + hmargin;            int x = wx + col * cc + wmargin;            g.setFont(font);            if (reverse) {                g.setGrayScale(255);            } else {                g.setGrayScale(0);            }            g.drawChar(c, x, y, Graphics.HCENTER | Graphics.TOP);        }        /**         * Show the cursor of this symbol table         *         * @param g The Graphics object to paint to         * @param pos The position of the cursor         * @param show A flag indicating the visibility of the cursor         */        void showCursor(Graphics g, int pos, boolean show) {            int row = pos / cols;            int col = pos % cols;            int y = wy + row * cc;            int x = wx + col * cc;            if (show) {                g.setGrayScale(0);            } else {                g.setGrayScale(255);            }            g.fillRect(x+margin, y+margin, cc-margin-1, cc-margin-1);            drawChar(g, symbolTableChars[pos], row, col, show);        }        /**         * Handle a key press event on this symbol table.	 * Overrides Canvas.keyPressed.         *         * @param keyCode The key that was pressed         */        protected void keyPressed(int keyCode) {            if ((keyCode > 0) && (((char)keyCode) == '*')) {                currentDisplay.setCurrent(previousScreen);            } else {                switch (getGameAction(keyCode)) {                case Canvas.RIGHT:                    if ((pos + 1) < symbolTableChars.length) {                        newpos = pos + 1;                        repaint();                    }                    break;                case Canvas.LEFT:                    if (pos > 0) {                        newpos = pos - 1;                        repaint();                    }                    break;                case Canvas.UP: {                    int p = pos - cols;                    if (p >= 0) {                        newpos = p;                        repaint();                    }                    break;                }                case Canvas.DOWN: {                    int p = pos + cols;                    if (p < symbolTableChars.length) {                        newpos = p;                        repaint();                    }                    break;                }                case Canvas.FIRE:                    buf[0] = symbolTableChars[pos];                    bufLen = 1;                    committedLen = 1;                    currentDisplay.setCurrent(previousScreen);                    break;                }            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -