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

📄 hanziinput.java

📁 j2me 汉字输入框
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   HanziInput.java

package util.hzpy;

import Finals;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

// Referenced classes of package src.util.hzpy:
//            StateToPinyinSequence, HanyuPinyin

public class HanziInput {
	private static HanziInput instance;

	private int input_state;

	private int state;

	private StringBuffer py_seq;

	private int i_of_py;

	private StringBuffer current_pinyin;

	private char c_hzSeq[];

	private int i_c_hz;

	private char current_hanzi;

	private int l_l;

	private int l_p_c_py;

	private HanziInput() {
		init();
	}

	public static HanziInput getInstance() {
		if (instance == null)
			instance = new HanziInput();
		return instance;
	}

	public void init() {
		state = 0;
		py_seq = new StringBuffer();
		current_pinyin = new StringBuffer();
		i_c_hz = 0;
		i_of_py = 0;
		l_l = 10;
		input_state = 1;
		l_p_c_py = 0;
	}

	public void nextKeycode(int keycode) {
		if (keycode < 0 || keycode == 42) {
			if (input_state > 2)
				doChoose(keycode);
		} else {
			update(keycode);
			updateInput(3);
		}
	}

	public void back() {
		state = state / 10;
		if (state > 1) {
			try {
				l_p_c_py--;
				updateInput(3);
			} catch (Exception e) {
			}
		} else {
			state = 1;
			init();
		}
	}

	private void update(int keyCode) {
		int key_s = getNumberOfKey(keyCode);
		if (key_s == -1)
			return;
		int temp_state = state * 10 + key_s;
		String temp_pinyin_sequence = getPinyinSequence(temp_state);
		if (temp_pinyin_sequence != null && temp_pinyin_sequence.length() > 0) {
			state = temp_state;
			py_seq = (new StringBuffer()).append(temp_pinyin_sequence);
			l_p_c_py++;
		}
	}

	private int getNumberOfKey(int keycode) {
		if (keycode > 49 && keycode < 58)
			return keycode - 48;
		else
			return -1;
	}

	private StringBuffer getCurrentPinyin() {
		StringBuffer result = new StringBuffer();
		int length = py_seq.length();
		if (length == 0)
			return null;
		char temp[] = new char[length];
		py_seq.getChars(0, length, temp, 0);
		int count_space = 0;
		for (int i = 0; i < length; i++) {
			char ch = temp[i];
			if (count_space == i_of_py) {
				if (ch == ' ')
					break;
				result.append(ch);
			}
			if (ch == ' ')
				count_space++;
		}

		if (result.length() == 0) {
			i_of_py = 0;
			result = getCurrentPinyin();
		}
		return result;
	}

	private String getPinyinSequence(int state) {
		String result = null;
		try {
			result = StateToPinyinSequence.getPinyinSequence(state);
		} catch (Exception e) {
		}
		return result;
	}

	private void doChoose(int keycode) {
		if (keycode == -3) {
			if (i_c_hz > 0)
				i_c_hz--;
			updateInput(6);
		} else if (keycode == -4) {
			if (i_c_hz < c_hzSeq.length - 1)
				i_c_hz++;
			else
				i_c_hz = 0;
			updateInput(6);
		} else if (keycode == -1) {
			if (i_c_hz - l_l >= 0)
				i_c_hz -= l_l;
			else
				i_c_hz = 0;
			updateInput(6);
		} else if (keycode == -2) {
			if (i_c_hz + l_l < c_hzSeq.length)
				i_c_hz += l_l;
			else
				i_c_hz = c_hzSeq.length - 1;
			updateInput(6);
		} else if (keycode == 42) {
			i_of_py++;
			i_c_hz = 0;
			updateInput(4);
		}
	}

	private void updateInput(int input_state) {
		if (input_state == 3) {
			py_seq = (new StringBuffer()).append(getPinyinSequence(state));
			this.input_state = 3;
			input_state = 4;
		}
		if (input_state == 4) {
			current_pinyin = getCurrentPinyin();
			this.input_state = 4;
			input_state = 5;
		}
		if (input_state == 5) {
			c_hzSeq = HanyuPinyin.getHanziSequence(current_pinyin);
			this.input_state = 5;
			input_state = 6;
		}
		if (input_state == 6) {
			current_hanzi = c_hzSeq[i_c_hz];
			this.input_state = 6;
		}
	}

	// 画面板
	public void updateView(Graphics g, int w, int h, Font f) {
		int w_c = f.charWidth(Finals.CHAR);
		l_l = w / w_c;
		int i_c_h = i_c_hz % l_l;
		int beg = (i_c_hz / l_l) * l_l;
		int end = beg + l_l;
		if (end >= c_hzSeq.length)
			end = c_hzSeq.length;
		char c_hSeq[] = new char[l_l];
		(new StringBuffer()).append(c_hzSeq).getChars(beg, end, c_hSeq, 0);
		// 显示区域
		g.setClip(0, h - 2 * f.getHeight(), w, h);
		g.setColor(0x000000);
		g.fillRect(0, 0, w, h);
		// 当前拼音
		g.setColor(0xffffff);
		g.setFont(f);
		int l_of_pySeq = py_seq.length();
		int c_of_py = 0;
		char temp_py[] = new char[l_p_c_py + 1];
		temp_py[l_p_c_py] = ' ';
		int i_py_of_seq = 0;
		do {
			if (i_py_of_seq >= l_of_pySeq)
				break;
			py_seq.getChars(i_py_of_seq, i_py_of_seq + l_p_c_py, temp_py, 0);
			if (c_of_py == i_of_py)
				//当前拼音
				g.setColor(0xffffff);
			else
				g.setColor(0xff0000);
			g.drawChars(temp_py, 0, l_p_c_py, c_of_py
					* f.charsWidth(temp_py, 0, l_p_c_py + 1), h - 2
					* f.getHeight(), 20);
			c_of_py++;
			i_py_of_seq += l_p_c_py;
			if (i_py_of_seq >= l_of_pySeq)
				break;
			while (py_seq.charAt(i_py_of_seq) != ' '
					&& ++i_py_of_seq < l_of_pySeq)
				;
			i_py_of_seq++;
		} while (true);
		g.setColor(0xff0000);
		g.drawChars(c_hSeq, 0, i_c_h, 0, h - f.getHeight(), 20);
		g.setColor(0xffffff);
		g.drawChars(c_hSeq, i_c_h, 1, i_c_h * w_c, h - f.getHeight(), 20);
		if (c_hSeq.length < 2)
			return;
		int left_length = l_l - i_c_h;
		if (left_length >= c_hSeq.length - i_c_h - 1)
			left_length = c_hSeq.length - i_c_h - 1;
		g.setColor(0xff0000);
		g.drawChars(c_hSeq, i_c_h + 1, left_length, (i_c_h + 1) * w_c, h
				- f.getHeight(), 20);
	}

	public int getInput_state() {
		return input_state;
	}

	public StringBuffer getPinyinSequence() {
		return py_seq;
	}

	public StringBuffer getCurrent_pinyin() {
		return current_pinyin;
	}

	public char[] getHanziSequence() {
		return c_hzSeq;
	}

	public char getCurrent_hanzi() {
		return current_hanzi;
	}
}

⌨️ 快捷键说明

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