我有一个类继承Canvas,做成全屏的,但我要在这个类中能接收用户输入文字,要怎么做啊?高手指点一二。谢谢
或者说怎么能把一个textbox放到Canvas上
如果你要输入英文和数字,自己写输入法
如果是中文,不可能~~
同意楼上的
xueyong1203(二当家的) ,能说说怎么写吗? 另外,Form能不能全屏呀,如果能的话,我用form代替canvas
自己写的意思说白了就是在canvas上画一个textbox,在按键响应中捕获按键,在画到相应的区域
yanhan0615(炮炮) 问题就是在按键响应中捕获按键中,因为一个键对应很多字符,
用一张包含你所有需要输入的字母和数字的图片,然后通过在图片上选择字母来输入.现在很多游戏,包括PC(当然PC大部分只是为了输入效果)都这样来做.
好象只有Sharp的个别型号支持输入中文,其他所有的手机都不支持
nokia支持全屏 fullcanvas 现在2.0也支持了吧
那你做的跟手机上一样就可以了,按键有一个延迟,如果在很短的时间(自己定义)内按的话是几个字符轮流跳动
自己做了一个能在canvas下接收输入的textbox,贴出来和大家共享一下,其中主要是参考了网上的代码,这个代码可以从这里下载http://www.cnblogs.com/Files/zhengyun_ustc/BloglinesMobile-src.rar。
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics;
public class MyTextBox extends Canvas implements Runnable{
private final static int KEY_CLEAR = -8; private final static int MAX_NUM = 10; private final static int KEY_LEFT = -3; private final static int KEY_RIGHT = -4;
private char[] m_text = new char[10]; private int currentCursor = 0; private int previouseCursor = 0; private int currentKey = -1; private int keyStatus = 0;
private int offPos = 0;
private int currentLen = 0; private boolean showCursor = true; private int left; private int top;
final static protected char[][] CHAR_OPTIONS = { {'0', '+'}, {'1', '.', '-', '\'', '+', '!', '\"', '*', '#', '$', '%', '(', ')', '@', '&', '_', '~', '`', '^', '=', '\\', '|', ',', '/', '?', '<', '>', ':', ';', '[', ']'}, {'A', 'B', 'C', 'a', 'b', 'c', '2'}, {'D', 'E', 'F', 'd', 'e', 'f', '3'}, {'G', 'H', 'I', 'g', 'h', 'i', '4'}, {'J', 'K', 'L', 'j', 'k', 'l', '5'}, {'M', 'N', 'O', 'm', 'n', 'o', '6'}, {'P', 'Q', 'R', 'S', 'p', 'q', 'r', 's', '7'}, {'T', 'U', 'V', 't', 'u', 'v', '8'}, {'W', 'X', 'Y', 'Z', 'w', 'x', 'y', 'z', '9'}, {' '} };
final static protected int[] KEY_OPTIONS = { KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND }; public MyTextBox(int left,int top){ this.left = left; this.top = top; for(int i = 0; i < 10; i++){ m_text[i] = '\0'; }
}
protected void paint(Graphics g) { g.setColor(255,255,255); g.fillRect(left,top,112,20);
drawBox(g);
drawChar(g);
if(showCursor) drawCursor(g);
}
private void drawBox(Graphics g){ g.setColor(0,0,0); g.drawRect(left,top,110,18); }
private void drawChar(Graphics g){ int lt = left + 4; int tp = top + 2; g.setColor(0,0,255); Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN ,Font.SIZE_MEDIUM); g.setFont(font); int fw ; for(int i = 0; i < 10; i++){ if(m_text[i] == '\0') break; fw = font.stringWidth(m_text[i] + ""); //System.out.println(fw + "," + m_text[i]); g.drawString(m_text[i] + "",lt ,tp,Graphics.LEFT | Graphics.TOP); lt = lt + fw + 2; } }
private void drawCursor(Graphics g){ Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN ,Font.SIZE_MEDIUM); int lt = left + 4; g.setColor(0,0,255); for(int i = 0; i < currentCursor; i++){ lt = lt + font.stringWidth(m_text[i] + "") + 2; } lt = lt -2; g.drawLine(lt,top + 4,lt,top + 15); }
private void rightCursor(){ if(currentCursor >= currentLen) return; currentCursor++; currentKey = -1; }
private void leftCursor(){ if(currentCursor <= 0) return; currentCursor--; currentKey = -1; }
private void deleteChar(){ if(currentCursor <= 0) return; if(currentCursor > currentLen ) return ; System.arraycopy(m_text,currentCursor ,m_text,currentCursor -1,currentLen - currentCursor); m_text[currentLen - 1] = '\0'; currentLen--; currentCursor--; if(currentCursor < 0) currentCursor = 0; if(currentLen < 0) currentLen = 0; }
private void insertChar(char ch,boolean mode){ //System.out.println("mode="+mode); if(mode){ if(currentLen >= MAX_NUM) return; System.arraycopy(m_text,currentCursor,m_text,currentCursor + 1,currentLen - currentCursor); m_text[currentCursor] = ch; previouseCursor = currentCursor; currentCursor++; currentLen++;
}else{ m_text[previouseCursor ] = ch; } }
protected int getKeyOption(int keyCode) { //System.out.println("getKeyOption>>" + keyCode); int l = KEY_OPTIONS.length; for (int keyIndex = 0; keyIndex < l; keyIndex++) { if (KEY_OPTIONS[keyIndex] == keyCode) return keyIndex; } return -1; }
private char getKeyCodeChar(int keyCode,boolean mode){ int keyIndex = getKeyOption(keyCode); if(keyIndex == -1) return '\0'; if(mode){ offPos++; int len = CHAR_OPTIONS[keyIndex].length; if(offPos >= len) offPos %= len; }else{ offPos = 0; } char ch = CHAR_OPTIONS[keyIndex][offPos]; return ch; }
public void run() { while(true){ try{ Thread.sleep(500); showCursor = !showCursor; }catch(Exception ex){
} synchronized(this){ keyStatus++; } repaint(); }
}
public void keyPressed(int keyCode){
switch(keyCode){ case KEY_LEFT: leftCursor(); break; case KEY_RIGHT: rightCursor(); break; case KEY_CLEAR: deleteChar(); break; case KEY_NUM0: case KEY_NUM1: case KEY_NUM2: case KEY_NUM3: case KEY_NUM4: case KEY_NUM5: case KEY_NUM6: case KEY_NUM7: case KEY_NUM8: case KEY_NUM9:
boolean b; //System.out.println(currentKey+","+keyStatus); if(currentKey == keyCode && keyStatus <= 1){ b = true; }else{ if(currentLen >= MAX_NUM){ return; } b = false; } char ch = getKeyCodeChar(keyCode,b); insertChar(ch,!b);
synchronized(this){ keyStatus = 0; } break; } repaint(); currentKey = keyCode; }
}
对于楼主的精神,赞一个,收下了
发现bug的话,别忘了告诉我
|