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

📄 defaultinputmethodhandler.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param keyCode The code of the key that was pressed     * @return boolean If True, this handler has handled the key press     */    public boolean keyPressed(int keyCode) {        int idx = toKeyMapIndex(keyCode);        if (idx == KEY_NONE) {            return false;        }        if (idx == KEY_CLEAR) {            setTimer(TM_CLEAR_BUFFER, 1500);            return true;        }        switch (constraint) {        case TextField.NUMERIC:            if (idx == KEY_STAR) {                toggleSign();                return true;            }            if (idx == KEY_POUND) {                return false;            }            break;        case TextField.PHONENUMBER:            break;        case TextField.EMAILADDR:        case TextField.URL:        case TextField.ANY:            if (idx == KEY_STAR) {                setTimer(TM_INPUT_MODE, 1000);                return true;            }            break;        }                if (processChar(idx)) {            handleCharInput();            return true;        }        return false;    }    /**     * Handle a key released event.     * Overrides InputMethodHandler.keyReleased.     *     * @param keyCode The code of the key that was released     * @return boolean If True, this handler has handled the key release     */    public boolean keyReleased(int keyCode) {        int idx = toKeyMapIndex(keyCode);        if (idx == KEY_NONE) {            return false;        }        if (idx == KEY_CLEAR) {            if (timerType == TM_CLEAR_BUFFER) {                endComposition();                int pos = imc.getCaretPosition();                if (pos > 0) {                    imc.replace(null, 0, 1, pos-1, pos);                }            }            return true;        }        if (idx == KEY_STAR) {            if (timerType == TM_INPUT_MODE) {                endComposition();                setInputMode(nextInputMode());            }            return true;        }        return false;    }    /**     * Handle a key repeated event.     * Overrides InputMethodHandler.keyRepeated.     *     * @param keyCode The code of the key that was repeated     * @return boolean If True, this handler has handled the key repeat     */    public boolean keyRepeated(int keyCode) {        return false;    }    /**     * End the composition of this handler.     * Overrides InputMethodHandler.endComposition.     */    public void endComposition() {        if (timerType != TM_NONE) {            cancelTimer();            timerType = TM_NONE;        }        if (incomposing) {            imc.setCaretVisible(true);            incomposing = false;        }	end();    }    /**     * Get the list of supported input modes of this handler.     * Overrides InputMethodHandler.supportedInputModes.     *     * @return String[] The array of supported input modes     */    public String[] supportedInputModes() {        return supportedInputModes;    }    /**     * Insert a character.     *     * @param c The character to insert     */    protected void insertChar(char c) {        if (imc.getSize() < imc.getMaxSize()) {            char[] buf = {c};            imc.insert(buf, 0, 1, imc.getCaretPosition());        }    }    /**     * Toggle the sign (+/-).     */    protected void toggleSign() {        char[] tmp = new char[imc.getSize() + 1];        int len = imc.getChars(tmp);        if (len > 0 && tmp[0] == '-') {            --len;            if (len == 0) {                imc.setChars(null, 0, 0);            } else {                System.arraycopy(tmp, 1, tmp, 0, len);                int pos = imc.getCaretPosition();                imc.setChars(tmp, 0, len);                imc.setCaretPosition(--pos);            }        } else {            if (len >= imc.getMaxSize()) {                return;            }            if (len != 0) {                System.arraycopy(tmp, 0, tmp, 1, len);            }            ++len;            tmp[0] = '-';            int pos = imc.getCaretPosition();            imc.setChars(tmp, 0, len);            imc.setCaretPosition(++pos);        }    }    /** Flag indicating this handler is composing text. */    protected boolean incomposing;    /** The text input position. */    protected int inputPos; // keep track of text input position    /**     * Handle character input.     */    protected void handleCharInput() {        if (bufLen == 0) {            return;        }        if (incomposing == false) {            inputPos = imc.getCaretPosition();        }        if (committedLen >= 1) {            if (incomposing == true) {                if ((imc.getSize() + committedLen) >= imc.getMaxSize()) {                    committedLen = imc.getMaxSize() - imc.getSize();                }                if (committedLen > 0) {                    imc.replace(buf, 0, committedLen, inputPos-1, inputPos);                }            } else {                for (int i = 0; i < committedLen; i++) {                    if (imc.getSize() < imc.getMaxSize()) {                        imc.insert(buf, i, i+1, inputPos+i);                    }                }            }            incomposing = false;        }        if (bufLen > 0 && bufLen > committedLen) {            if (incomposing == true) {                imc.replace(buf, committedLen, 1, inputPos-1, inputPos);            } else {                if (imc.getSize() < imc.getMaxSize()) {                    imc.insert(buf, committedLen, 1, inputPos++);                    incomposing = true;                }            }        }        if (incomposing == true) {            setTimer(TM_IN_COMPOSING, 1600);        } else {            imc.setCaretVisible(true);        }    }    /** The '0' Timer. */    protected final static int TM_NONE         = 0;    /** The input mode timer. */    protected final static int TM_INPUT_MODE   = 1;    /** The 'in composition' timer. */    protected final static int TM_IN_COMPOSING = 2;    /** The 'clear buffer' timer. */    protected final static int TM_CLEAR_BUFFER = 3;    /** The type of timer to set. */    protected int timerType;    /**     * Set a new timer.     *     * @param type The type of Timer to set     * @param delay The length of delay for the Timer     */    protected void setTimer(int type, long delay) {        if (type != timerType) {            if (type == TM_IN_COMPOSING) {                imc.setCaretVisible(false);            }            timerType = type;        }        cancelTimer();        try {            timerClient = new TimerClient();            timerService.schedule(timerClient, delay);        } catch (IllegalStateException e) {            e.printStackTrace();            cancelTimer();        }    }    /** The Timer to service TimerTasks. */    protected Timer timerService = new Timer();    /** A TimerTask. */    protected TimerTask timerClient = null;    /**     * A special TimerTask class     */    class TimerClient extends TimerTask {        /**         * Simply calls timerWentOff()         */        public final void run() {            timerWentOff();        }    }    /**     * Cancel any running Timer.     */    protected void cancelTimer() {        if (timerClient != null) {            timerClient.cancel();            timerClient = null;        }    }    /**     * Called by the TimerTask when a Timer executes.     */    protected void timerWentOff() {        switch (timerType) {        case TM_INPUT_MODE:            endComposition();            nextInputMode = inputMode;            setInputMode(IM_SYMBOL);            break;        case TM_IN_COMPOSING:            endComposition();            break;        case TM_CLEAR_BUFFER:            endComposition();            imc.setChars(null, 0, 0);            break;        }    }    /** The last key and last position of the input. */    protected int lastKey, lastPos;    /**     * Process a character.     *     * @param idx The index into the keymap     * @return boolean     */    protected boolean processChar(int idx) {	/*	 * 1. To start from the initial state.  */	if (lastKey == -1) {	    if (inputMode == IM_NONE) {		buf[0] = 0;		bufLen = committedLen = 0;            } else {                buf[0] = keyMap[idx][0];                if (buf[0] == '\0') {                    return false;                }                if (keyMap[idx].length == 1) {		    bufLen = committedLen = 1;                } else {                    bufLen = 1;		    lastKey = idx;		    committedLen = 0;		    lastPos = 0;                }            }	    return true;	} else	/*	 * 2. To commit the previous key and send the current key.	 */        if (lastKey != idx) {            char c = keyMap[idx][0];	    if (c == '\0') {		return false;	    }	    buf[0] = keyMap[lastKey][lastPos];            buf[1] = c;            bufLen = 2;            lastPos = 0;            if (keyMap[idx].length == 1) {	        committedLen = 2;	        lastKey = -1;            } else {	        committedLen = 1;	        lastKey = idx;            }	    return true;	} else 	/*	 * 3. To send the next candidate at the same key position.	 */        if (lastKey == idx) {	    int len = keyMap[idx].length;	    if (len == (lastPos+1)) {		lastPos = 0;	    } else {		lastPos += 1;	    }	    buf[0] = keyMap[idx][lastPos];	    bufLen = 1;	    committedLen = 0;	    return true;	}	return false;    }    /**     * End the composition.     */    protected void end() {	if (lastKey != -1) {	    buf[0] = keyMap[lastKey][lastPos];	    bufLen = 1;	    committedLen = 1;	    lastKey = -1;	} else {	    bufLen = 0;	    committedLen = 0;	}    }    /**     * Get the next input mode.     *     * @return int The next input mode     */    protected int nextInputMode() {        if (nextInputMode != IM_NONE) {            int tmp = nextInputMode;            nextInputMode = IM_NONE;            return tmp;        }

⌨️ 快捷键说明

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