J2ME simple digital input with low-level interface

Source: Internet
Author: User
Tags stringbuffer

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.