Recently in writing a J2ME program which includes a digital input textfield on the phone to implement (S40 Simulator) The number of input is very cumbersome, so want to avoid this problem and then switch to low-level interface implementation:
Private StringBuffer sb=new stringbuffer ();//define a stringbuffer to hold the input number
private int len=4;//The length of the number you want to enter
Private int temp;//Fetch number
protected void paint (Graphics g)
{
G.setcolor (white);
G.fillrect (0,0,getwidt H (), getheight ());
G.setcolor (black);
g.DrawString (sb.tostring (), GetWidth ()/2,getheight ()/2,graphics.top| Graphics.hcenter); Display input
}
protected void keypressed (int keynum)
{
if (sb.length () ==len)//Start key feature description
{
Switch (keynu m)
{
Case Key_star:
Temp=integer.parseint (Sb.tostring ());///Get the number entered in the screen and then you can use the number to press the * start kinetic energy
break;
Default:
break;
}
}
if (Sb.length () {
switch (keynum)
{
Case KEY_NUM1:
Sb.append ("1");
break;
Case key_num2 :
Sb.append ("2");
break;
Case KEY_NUM3:
Sb.append ("3"),
Break,
Case KEY_NUM4:
Sb.append ("4"),
Break,
Case key_num5:< br> sb.append ("5");
break;
Case KEY_NUM6:
Sb.append ("6"),
Break,
Case Key_NUM7:
Sb.append ("7");
Break,
Case KEY_NUM8:
Sb.append ("8"),
Break,
Case KEY_NUM9:
Sb.append ("9");
break;
Case KEY_NUM0:
Sb.append ("0"),
break;
Default:break;
}
}
if (Sb.length () >0)//Backspace key function Description
{
if (keynum==key_pound) Sb.deletecharat (Sb.length ()-1);
}
Repaint ();
}
protected void keyreapeated (int keynum)
{
if (sb.length () >0)//long by a one-time removal constructor description
if (keynum==key_ POUND) Sb=null; }
Repaint ();
}
Note: keyreapeated () method Jtwi does not make hard rules, so we must use the Canvas.hasrepeatedevents () method in the development of the actual detection.