Skip to content

Commit 84d2fdb

Browse files
committed
add TextField input support
1 parent f5aaeb1 commit 84d2fdb

5 files changed

Lines changed: 116 additions & 6 deletions

File tree

src/javax/microedition/lcdui/Displayable.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,37 @@ public void renderItems()
148148
{
149149
if(currentItem == i)
150150
{
151-
gc.fillRect(0,y,width,15);
151+
gc.fillRect(0,y,width,20);//选中为黑底白字
152152
gc.setColor(0xFFFFFF);
153+
154+
if(items.get(i) instanceof TextField)
155+
{
156+
Mobile.setTextField(((TextField)items.get(i)));
157+
}
158+
else
159+
{
160+
Mobile.setTextField(null);
161+
}
162+
}
163+
gc.drawString(items.get(i).getLabel()+":", 10, y, Graphics.LEFT);
164+
165+
gc.setColor(0x000000);
166+
if(items.get(i) instanceof TextField)
167+
{
168+
y+=20;//防止label和text位置重叠
169+
gc.drawString(((TextField)items.get(i)).getString(), width/2, y, Graphics.HCENTER);
153170
}
154-
gc.drawString(items.get(i).getLabel(), width/2, y, Graphics.HCENTER);
171+
155172
if(items.get(i) instanceof StringItem)
156173
{
174+
//这个地方应该会导致label和string位置重叠,由于没遇到满足情况的jar,具体表现不清楚
157175
gc.drawString(((StringItem)items.get(i)).getText(), width/2, y, Graphics.HCENTER);
158176
}
159-
gc.setColor(0x000000);
160177
if(items.get(i) instanceof ImageItem)
161178
{
162179
gc.drawImage(((ImageItem)items.get(i)).getImage(), width/2, y, Graphics.HCENTER);
163180
}
164-
y+=15;
181+
y+=20;
165182
}
166183
}
167184
// Draw Commands

src/javax/microedition/lcdui/TextField.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,41 @@ public void insert(String src, int position)
8888
text = out.toString();
8989
}
9090

91+
public void append(char c)
92+
{
93+
if(text.length()>=max)
94+
{
95+
return;
96+
}
97+
98+
StringBuilder out = new StringBuilder(text);
99+
out.append(c);
100+
101+
text = out.toString();
102+
}
103+
104+
public void replace(char c)//替换最后一个字符
105+
{
106+
StringBuilder out = new StringBuilder(text);
107+
if (out.length() > 0) { // 检查长度避免空指针异常
108+
out.setCharAt(out.length() - 1, c);
109+
}
110+
else
111+
{
112+
out.append(c);
113+
}
114+
text = out.toString();
115+
}
116+
117+
public void delete()
118+
{
119+
StringBuilder out = new StringBuilder(text);
120+
if (out.length() > 0) { // 检查长度避免空指针异常
121+
out.delete(out.length() - 1, out.length()); // 删除最后一个字符
122+
}
123+
text = out.toString();
124+
}
125+
91126
public void setChars(char[] data, int offset, int length)
92127
{
93128
StringBuilder out = new StringBuilder();

src/org/recompile/freej2me/Anbu.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,15 @@ public void start()
413413
proc=processBuilder.start();
414414

415415
//标准输出流,从接口获取用户的按键输入
416-
keys = proc.getInputStream(); // miyoo mini/x64-linux
417-
//keys = proc.getErrorStream(); //gkdminiplus
416+
//keys = proc.getInputStream(); // miyoo mini/x64-linux
417+
keys = proc.getErrorStream(); //gkdminiplus
418+
//先把错误流里的数据清空
419+
InputStreamReader eisr = new InputStreamReader(keys);
420+
BufferedReader procErrorOutput = new BufferedReader(eisr);
421+
String output;
422+
while (procErrorOutput.ready() && (output = procErrorOutput.readLine()) != null) {
423+
//System.out.println(output);
424+
}
418425

419426
//输入流,把图像传给接口
420427
frame = proc.getOutputStream();

src/org/recompile/mobile/Mobile.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import javax.microedition.lcdui.Display;
2222
import javax.microedition.lcdui.Canvas;
23+
import javax.microedition.lcdui.TextField;
2324
import javax.microedition.m3g.Graphics3D;
2425

2526
/*
@@ -36,6 +37,8 @@ public class Mobile
3637

3738
private static Display display;
3839

40+
private static TextField tf=null;
41+
3942
//private static Graphics3D graphics3d;
4043

4144
/* public static boolean quiet = false;
@@ -122,6 +125,16 @@ public static void setDisplay(Display d)
122125
display = d;
123126
}
124127

128+
public static void setTextField(TextField t)
129+
{
130+
tf = t;
131+
}
132+
133+
public static TextField getTextField()
134+
{
135+
return tf;
136+
}
137+
125138
/* public static Graphics3D getGraphics3D()
126139
{
127140
return graphics3d;

src/org/recompile/mobile/MobilePlatform.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.microedition.lcdui.game.GameCanvas;
2727
import javax.microedition.lcdui.Image;
2828
import javax.microedition.m3g.Graphics3D;
29+
import javax.microedition.lcdui.TextField;
2930

3031
import java.awt.image.BufferedImage;
3132

@@ -56,6 +57,9 @@ public class MobilePlatform
5657
private int[] keyStateArr=new int[6];
5758
private int s=0;
5859
private int e=0;
60+
61+
private static int chr=-1;
62+
private static int numchr=-1;
5963

6064
public boolean suppressKeyEvents=false;
6165

@@ -123,6 +127,40 @@ public void setPainter(Runnable r)
123127

124128
public void keyPressed(int keycode)
125129
{
130+
TextField tf=Mobile.getTextField();
131+
if(tf!=null)
132+
{
133+
if(keycode==Mobile.KEY_NUM6 || keycode==Mobile.NOKIA_RIGHT)//替换a-z字符
134+
{
135+
chr=(chr+1)%26;
136+
tf.replace((char)(chr+0x61));
137+
}
138+
else if(keycode==Mobile.KEY_NUM4 || keycode==Mobile.NOKIA_LEFT)//替换a-z字符
139+
{
140+
chr=(chr+25)%26;
141+
tf.replace((char)(chr+0x61));
142+
}
143+
if(keycode==Mobile.KEY_NUM3)//替换0-9字符
144+
{
145+
numchr=(numchr+1)%10;
146+
tf.replace((char)(numchr+0x30));
147+
}
148+
else if(keycode==Mobile.KEY_NUM1)//替换0-9字符
149+
{
150+
numchr=(numchr+9)%10;
151+
tf.replace((char)(numchr+0x30));
152+
}
153+
else if(keycode==Mobile.KEY_STAR)//按*删除
154+
{
155+
tf.delete();
156+
}
157+
else if(keycode==Mobile.KEY_POUND)//按#添加新字符
158+
{
159+
tf.append((char)(chr+0x61));
160+
}
161+
162+
}
163+
126164
updateKeyState(keycode, 1);
127165
if(!suppressKeyEvents)
128166
Mobile.getDisplay().getCurrent().keyPressed(keycode);

0 commit comments

Comments
 (0)