JavaMe开发:绘制文本框TextEdit

开发 后端
在JavaMe连载(3)-也说MVC设计模式 一文中提到了一个TextEdit类,但没有给出具体实现,TextEdit是采用GameCanvas绘制的文本编辑器。本文结合实例给出实现的方法。

【问题描述】在JavaMe连载(3)-也说MVC设计模式 一文中提到了一个TextEdit类,但没有给出具体实现,TextEdit是采用GameCanvas绘制的文本编辑器。本文结合实例给出实现的方法。

 

【原理】

1 运用Graphics、GameCanvas绘制文本框和光标。

2 检测到输入事件时,跳转到 高级界面->TextBox 。通过系统调用输入法完成输入。

3 将TextBox输入的值返回给TextEdit对象。

 

【设计模式】

这个过程有点类似装饰模式,实际上,实现输入的还是TextBox,只是给TextBox装饰了一下,形成了一个漂亮的外观。

 

【代码清单】

TextEdit.java

  1. package com.token.view.components;  
  2.  
  3. import javax.microedition.lcdui.Font;  
  4. import javax.microedition.lcdui.Graphics;  
  5. import javax.microedition.lcdui.game.GameCanvas;  
  6.  
  7. public class TextEdit extends GameCanvas  
  8. {  
  9.     private Font ft;  
  10.       
  11.     public int width;  
  12.     public int height;  
  13.       
  14.     public TextEdit(GameCanvas canvas)   
  15.     {  
  16.         super(false);  
  17.           
  18.     }  
  19.       
  20.     //绘制文本框  
  21.     public void drawTextBox(GameCanvas canvas, Graphics graphics, String text, int x, int y, boolean cursorBlinkOn)  
  22.     {  
  23.         //System.out.println("draw");  
  24.         int padding = 4;  
  25.         int margin = 2;  
  26.           
  27.         ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);  
  28.         graphics.setFont(ft);  
  29.           
  30.         width = 3*canvas.getWidth()/5+2*padding;  
  31.         height = ft.getHeight()+2*padding;  
  32.  
  33.         graphics.setColor(Color.frame);  
  34.         graphics.fillRect(x+1,y+1,width+margin,height+margin);  
  35.           
  36.         graphics.setColor(Color.frameBg);  
  37.         graphics.drawRect(x, y,width, height);  
  38.         graphics.setColor(Color.background);  
  39.         graphics.fillRect(x+1, y+1,width-1,height-1);  
  40.           
  41.         graphics.setColor(Color.text);  
  42.         graphics.drawString(text, x+padding, y+padding, Graphics.TOP|Graphics.LEFT);  
  43.           
  44.         drawCursor(graphics, x+ft.stringWidth(text)+padding, y+padding, 1, ft.getHeight(), cursorBlinkOn);  
  45.           
  46.         canvas.flushGraphics(x,y,width,height);  
  47.     }  
  48.  
  49.  
  50.     //绘制光标  
  51.     public void drawCursor(Graphics graphics, int x, int y, int width, int height, boolean cursorBlinkOn)  
  52.     {  
  53.         if(cursorBlinkOn)  
  54.         {  
  55.             ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);  
  56.             graphics.setFont(ft);  
  57.             graphics.setColor(0x0,0x0,0x0);  
  58.             graphics.drawLine(x+width,y,x+width,y+height);  
  59.         }  
  60.     }  
  61. }  

PopUpTextBox.java

  1. package com.token.view;  
  2.  
  3. import javax.microedition.lcdui.Command;  
  4. import javax.microedition.lcdui.CommandListener;  
  5. import javax.microedition.lcdui.Displayable;  
  6. import javax.microedition.lcdui.TextBox;  
  7.  
  8. import com.token.util.UIController;  
  9.  
  10. public class PopUpTextBox extends TextBox {  
  11.  
  12.       private UIController controller;  
  13.       protected String canvasText = "";  
  14.       private Command okCommand;  
  15.       private Command cancelCommand;  
  16.       private String object_name = null;  
  17.       private String editor = null;  
  18.       private Object args_t[];  
  19.       private Object[] args;  
  20.         
  21.       
  22.       public PopUpTextBox(UIController control, String title, String text, int maxsize, int constraints)   
  23.       {  
  24.           super(title, text, maxsize, constraints);  
  25.           controller = control;  
  26.             
  27.           args = new Object[6];  
  28.             
  29.           okCommand = new Command("确定", Command.OK, 1);  
  30.           cancelCommand = new Command("取消", Command.CANCEL, 1);  
  31.             
  32.           this.addCommand(okCommand);  
  33.           this.addCommand(cancelCommand);  
  34.           this.setCommandListener(new TextBoxListener());  
  35.       }  
  36.         
  37.       public void init(Object[] args)  
  38.       {  
  39.           object_name = ((String)args[0]!=null)?(String)args[0]:"";  
  40.           editor = ((String)args[1]!=null)?(String)args[1]:"";  
  41.           //System.out.println(object_name);  
  42.           //System.out.println(editor);  
  43.           args_t = args;  
  44.           this.setString("");  
  45.       }  
  46.             
  47.       protected void closeTextBox(boolean update) {  
  48.             if (update) canvasText = this.getString();  
  49.             //System.out.println(canvasText);  
  50.  
  51.             if(object_name.equals("registScreen"))  
  52.             {  
  53.                 if(editor.equals("regist_name"))  
  54.                 {  
  55.                     if(args_t[3]!=""||args_t[3]!=null||args_t[4]!=""||args_t[4]!=null)  
  56.                     {  
  57.                         args[0] = object_name;  
  58.                         args[1] = editor;  
  59.                         args[2] = this.canvasText;  
  60.                         args[3] = args_t[3];  
  61.                         args[4] = args_t[4];  
  62.                     }  
  63.                     controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);  
  64.                 }  
  65.                 else if(editor.equals("regist_passwd"))  
  66.                 {  
  67.                     if(args_t[2]!=""||args_t[2]!=null||args_t[4]!=""||args_t[4]!=null)  
  68.                     {  
  69.                         args[0] = object_name;  
  70.                         args[1] = editor;  
  71.                         args[2] = args_t[2];  
  72.                         args[3] = this.canvasText;  
  73.                         args[4] = args_t[4];  
  74.                     }  
  75.                     controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);  
  76.                 }  
  77.                 else if(editor.equals("regist_passwd_re"))  
  78.                 {  
  79.                     if(args_t[2]!=""||args_t[2]!=null||args_t[3]!=""||args_t[3]!=null)  
  80.                     {  
  81.                         args[0] = object_name;  
  82.                         args[1] = editor;  
  83.                         args[2] = args_t[2];  
  84.                         args[3] = args_t[3];  
  85.                         args[4] = this.canvasText;  
  86.                     }  
  87.                     controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);  
  88.                 }  
  89.             }  
  90.               
  91.            //...  
  92.     }  
  93.         
  94.      private class TextBoxListener implements CommandListener  
  95.      {   
  96.         public void commandAction(Command command, Displayable disp)  
  97.         {  
  98.             if(command==okCommand)  
  99.             {  
  100.                 closeTextBox(true);  
  101.             }  
  102.             else if(command==cancelCommand)  
  103.             {  
  104.                 closeTextBox(false);  
  105.             }  
  106.         }  
  107.     }  
  108. }  

#p#

UserRegist.java

  1. package com.token.view;  
  2.  
  3. import javax.microedition.lcdui.Font;  
  4. import javax.microedition.lcdui.Graphics;  
  5. import javax.microedition.lcdui.game.GameCanvas;  
  6. import com.token.model.*;  
  7. import com.token.util.*;  
  8. import com.token.view.components.*;  
  9.  
  10. public class UserRegist extends GameCanvas implements Runnable {  
  11.  
  12.     private UIController controller;  
  13.     private Graphics graphics;  
  14.       
  15.     private Font ft;  
  16.       
  17.     private Menu menu;  
  18.     private Head head;  
  19.     private BackGroud backGroud;  
  20.       
  21.     private UserDataRecord userRecord;  
  22.  
  23.     private String title;  
  24.       
  25.     private TextEdit textEdit_name;  
  26.     private TextEdit textEdit_passwd;  
  27.     private TextEdit textEdit_passwd_re;  
  28.     private int textEdit_name_x;  
  29.     private int textEdit_name_y;  
  30.     private int textEdit_passwd_x;  
  31.     private int textEdit_passwd_y;  
  32.     private int textEdit_passwd_re_x;  
  33.     private int textEdit_passwd_re_y;  
  34.     private int currentlySelectedIndex = 0;  
  35.       
  36.     private String username;  
  37.     private String passwd;  
  38.     private String passwd_re;  
  39.       
  40.     long caretBlinkDelay = 500L;  
  41.     long lastCaretBlink = 0;  
  42.     private String object_name;  
  43.     private String editor;  
  44.     private boolean cursorBlinkOn1;  
  45.     private boolean cursorBlinkOn2;  
  46.     private boolean cursorBlinkOn3;  
  47.       
  48.     private int width;  
  49.     private int height;  
  50.       
  51.     public UserRegist(UIController control)   
  52.     {  
  53.         super(false);  
  54.         this.controller=control;  
  55.         this.title = "用户注册";  
  56.         setFullScreenMode(true);  
  57.         graphics = getGraphics();  
  58.           
  59.         width = getWidth();  
  60.         height = getHeight();  
  61.           
  62.         menu = new Menu(this);  
  63.         head = new Head(this);  
  64.         backGroud = new BackGroud(this);  
  65.           
  66.         userRecord = new UserDataRecord();  
  67.           
  68.         textEdit_name = new TextEdit(this);  
  69.         textEdit_passwd = new TextEdit(this);  
  70.         textEdit_passwd_re = new TextEdit(this);  
  71.     }  
  72.  
  73.     public void show(Object[] args) {  
  74.         // TODO Auto-generated method stub  
  75.         setFullScreenMode(true);  
  76.           
  77.         object_name = ((String)args[0]!=null)?(String)args[0]:"";  
  78.         editor = ((String)args[1]!=null)?(String)args[1]:"";  
  79.         username = ((String)args[2]!=null)?(String)args[2]:"";  
  80.         passwd = ((String)args[3]!=null)?(String)args[3]:"";  
  81.         passwd_re = ((String)args[4]!=null)?(String)args[4]:"";  
  82.           
  83.         if(editor.equals("regist_name"))  
  84.         {  
  85.             cursorBlinkOn1 = true;  
  86.             cursorBlinkOn2 = false;  
  87.             cursorBlinkOn3 = false;  
  88.             currentlySelectedIndex =0;  
  89.         }  
  90.         else if(editor.equals("regist_passwd"))  
  91.         {  
  92.             cursorBlinkOn1 = false;  
  93.             cursorBlinkOn2 = true;  
  94.             cursorBlinkOn3 = false;  
  95.             currentlySelectedIndex =1;  
  96.         }  
  97.         else if(editor.equals("regist_passwd_re"))  
  98.         {  
  99.             cursorBlinkOn1 = false;  
  100.             cursorBlinkOn2 = false;  
  101.             cursorBlinkOn3 = true;  
  102.             currentlySelectedIndex =2;  
  103.         }  
  104.           
  105.         //System.out.println(object_name);  
  106.         //System.out.println(editor);  
  107.         draw();  
  108.         redraw();  
  109.     }  
  110.  
  111.     public void draw()  
  112.     {  
  113.         //clearScreen();  
  114.         backGroud.drawBackGroud(this, graphics);  
  115.         head.drawHead(this,graphics,this.title);  
  116.         menu.drawMenu(this,graphics,"下一步","退出");  
  117.         drawBody();  
  118.     }  
  119.  
  120.     private void redraw()  
  121.     {  
  122.         switch(currentlySelectedIndex)  
  123.         {  
  124.             case 0:  
  125.             {  
  126.                 cursorBlinkOn2 = false;  
  127.                 cursorBlinkOn3 = false;  
  128.                 editor = "regist_name";  
  129.                 break;  
  130.             }  
  131.             case 1:  
  132.             {  
  133.                 cursorBlinkOn1 = false;  
  134.                 cursorBlinkOn3 = false;  
  135.                 editor = "regist_passwd";  
  136.                 break;  
  137.             }  
  138.             case 2:  
  139.             {  
  140.                 cursorBlinkOn1 = false;  
  141.                 cursorBlinkOn2 = false;  
  142.                 editor = "regist_passwd_re";  
  143.                 break;  
  144.             }  
  145.             default:;  
  146.         }  
  147.           
  148.         textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);  
  149.         textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);  
  150.         textEdit_passwd.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);  
  151.         textEdit_name.flushGraphics();  
  152.     }  
  153.  
  154.     public void drawBody()  
  155.     {  
  156.         int margin =5;  
  157.         ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);  
  158.           
  159.         String info = "用户名:\n";  
  160.         String info_wrap1[] = StringDealMethod.format(info, width-10, ft);  
  161.           
  162.         graphics.setFont(ft);  
  163.         graphics.setColor(Color.text);  
  164.         for(int i=0; i<info_wrap1.length; i++)  
  165.         {  
  166.             graphics.drawString(info_wrap1[i],5, (i) * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);  
  167.         }  
  168.           
  169.         textEdit_name_x = 5;  
  170.         textEdit_name_y = info_wrap1.length * ft.getHeight()+40;  
  171.         textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);  
  172.           
  173.         info = "用户密码:\n";  
  174.         String info_wrap2[] = StringDealMethod.format(info, width-10, ft);  
  175.       
  176.         graphics.setFont(ft);  
  177.         graphics.setColor(Color.text);  
  178.         for(int i=0; i<info_wrap2.length; i++)  
  179.         {  
  180.             graphics.drawString(info_wrap2[i],5, (i+info_wrap1.length) * ft.getHeight()+textEdit_name.height+margin+40, Graphics.TOP|Graphics.LEFT);  
  181.         }  
  182.           
  183.         textEdit_passwd_x = 5;  
  184.         textEdit_passwd_y = (info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+margin+40;  
  185.         textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);  
  186.       
  187.         info = "密码确认:\n";  
  188.         String info_wrap3[] = StringDealMethod.format(info, width-10, ft);  
  189.       
  190.         graphics.setFont(ft);  
  191.         graphics.setColor(Color.text);  
  192.         for(int i=0; i<info_wrap3.length; i++)  
  193.         {  
  194.             graphics.drawString(info_wrap3[i],5, (i+info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40, Graphics.TOP|Graphics.LEFT);  
  195.         }  
  196.           
  197.         textEdit_passwd_re_x = 5;  
  198.         textEdit_passwd_re_y = (info_wrap1.length+info_wrap2.length+info_wrap3.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40;  
  199.         textEdit_passwd_re.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);  
  200.       
  201.           
  202.     }  
  203.  
  204.     public void clearScreen()  
  205.     {  
  206.         graphics.setColor(0xff,0xff,0xff);  
  207.         graphics.fillRect(00, width, height);  
  208.     }  
  209.  
  210.     public void checkTimeStamp()  
  211.     {  
  212.         long currentTime = System.currentTimeMillis();  
  213.         //System.out.println("1");  
  214.         if(lastCaretBlink + caretBlinkDelay < currentTime)  
  215.         {  
  216.             //System.out.println("2");  
  217.             if(editor.equals("regist_name"))  
  218.             {  
  219.                 cursorBlinkOn1 =! cursorBlinkOn1;  
  220.                 cursorBlinkOn2 = false;  
  221.                 cursorBlinkOn3 = false;  
  222.             }  
  223.             else if(editor.equals("regist_passwd"))  
  224.             {  
  225.                 cursorBlinkOn1 = false;  
  226.                 cursorBlinkOn2 =! cursorBlinkOn2;  
  227.                 cursorBlinkOn3 = false;  
  228.             }  
  229.             else if(editor.equals("regist_passwd_re"))  
  230.             {  
  231.                 cursorBlinkOn1 = false;  
  232.                 cursorBlinkOn2 = false;  
  233.                 cursorBlinkOn3 =! cursorBlinkOn3;  
  234.             }  
  235.             lastCaretBlink = currentTime;  
  236.         }  
  237.     }  
  238.  
  239.     public void run()  
  240.     {  
  241.         //System.out.println("run");  
  242.         while(true)  
  243.         {  
  244.             checkTimeStamp();  
  245.               
  246.             redraw();  
  247.             try   
  248.             {  
  249.                 synchronized(this)  
  250.                 {  
  251.                     //System.out.println("3");  
  252.                     wait(50L);  
  253.                 }             
  254.             }  
  255.             catch(Exception e)  
  256.             {  
  257.                 e.printStackTrace();  
  258.             }  
  259.               
  260.         }  
  261.     }  
  262.  
  263.     protected void keyPressed(int keyCode)  
  264.     {  
  265.         switch(keyCode)  
  266.         {  
  267.             case KeyID.SOFT_RIGHT:  
  268.             {  
  269.                 controller.handleEvent(UIController.EventID.EVENT_EXIT,null);  
  270.                 break;  
  271.             }  
  272.             case KeyID.SOFT_LEFT:  
  273.             {  
  274.                 if(username!="" && passwd!=""&&passwd_re!="")  
  275.                 {  
  276.                     if(passwd.equals(passwd_re))  
  277.                     {  
  278.                           
  279.                           
  280.                         userRecord.db_deleteAllRecord();  
  281.                         if(userRecord.db_getRecord(1)==null)  
  282.                         {  
  283.                             UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());  
  284.                             userRecord.db_addRecord(userItem);  
  285.                             userItem = null;  
  286.                             System.gc();  
  287.                         }  
  288.                           
  289.                         String update = "start";  
  290.                         Object [] args = {"activeScreen"null, update};  
  291.                         controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_TOKEN_SCREEN,args);  
  292.                     }  
  293.                 }  
  294.                 break;  
  295.             }  
  296.             case KeyID.KEY_EDIT:  
  297.             case KEY_NUM0:  
  298.             case KEY_NUM1:  
  299.             case KEY_NUM2:  
  300.             case KEY_NUM3:  
  301.             case KEY_NUM4:  
  302.             case KEY_NUM5:  
  303.             case KEY_NUM6:  
  304.             case KEY_NUM7:  
  305.             case KEY_NUM8:  
  306.             case KEY_NUM9:  
  307.             {  
  308.                 //System.out.println(editor);  
  309.                 Object[] args = {object_name,editor,username,passwd,passwd_re};  
  310.                 controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);  
  311.                 break;  
  312.             }  
  313.             default:;  
  314.         }  
  315.               
  316.         keyCode = getGameAction(keyCode);  
  317.         switch(keyCode)  
  318.         {  
  319.             case UP:  
  320.             case LEFT:  
  321.             {  
  322.                 currentlySelectedIndex--;  
  323.                 if(currentlySelectedIndex<0)  
  324.                 {  
  325.                     currentlySelectedIndex=0;  
  326.                 }  
  327.                 else 
  328.                 {  
  329.                     redraw();  
  330.                 }  
  331.                 break;  
  332.             }  
  333.             case DOWN:  
  334.             case RIGHT:  
  335.             {  
  336.                 currentlySelectedIndex++;  
  337.                 if(currentlySelectedIndex>2)  
  338.                 {  
  339.                     currentlySelectedIndex=2;  
  340.                 }  
  341.                 else 
  342.                 {  
  343.                     redraw();  
  344.                 }  
  345.                   
  346.                 break;  
  347.             }  
  348.         }  
  349.     }  
  350.  
  351. }  
  352.  

#p#

【分析】

1 文本框的绘制(TextEdit.java)

需要传递GameCanvas、Graphics对象,实现绘图,策略是谁使用,谁传递该参数。此外需要床底文本框左上角坐标(x,y)以及控制光标闪烁的变量cursorBlinkOn。

  1. public void drawTextBox(GameCanvas canvas, Graphics graphics, String text, int x, int y, boolean cursorBlinkOn)  
  2. {  
  3.     //System.out.println("draw");  
  4.     int padding = 4;  
  5.     int margin = 2;  
  6.       
  7.     ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);  
  8.     graphics.setFont(ft);  
  9.       
  10.     width = 3*canvas.getWidth()/5+2*padding;  
  11.     height = ft.getHeight()+2*padding;  
  12.  
  13.     graphics.setColor(Color.frame);  
  14.     graphics.fillRect(x+1,y+1,width+margin,height+margin);  
  15.           
  16.     graphics.setColor(Color.frameBg);  
  17.     graphics.drawRect(x, y,width, height);  
  18.     graphics.setColor(Color.background);  
  19.     graphics.fillRect(x+1, y+1,width-1,height-1);  
  20.           
  21.     graphics.setColor(Color.text);  
  22.     graphics.drawString(text, x+padding, y+padding, Graphics.TOP|Graphics.LEFT);  
  23.           
  24.     drawCursor(graphics, x+ft.stringWidth(text)+padding, y+padding, 1, ft.getHeight(), cursorBlinkOn);  
  25.           
  26.     canvas.flushGraphics(x,y,width,height);  

2 绘制光标(TextEdit.java)

  1. public void drawCursor(Graphics graphics, int x, int y, int width, int height, boolean cursorBlinkOn)  
  2. {  
  3.     if(cursorBlinkOn)  
  4.     {  
  5.         ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);  
  6.         graphics.setFont(ft);  
  7.         graphics.setColor(0x0,0x0,0x0);  
  8.         graphics.drawLine(x+width,y,x+width,y+height);  
  9.     }  

3 实现光标闪烁

光标闪烁的实现需要用到线程,在UIController.java类中,需要绘制文本框的视图类,需要实现线程接口。

UIController.java

  1. case EventID.EVENT_NEXT_USER_REGIST_SCREEN:  
  2. case EventID.EVENT_USER_REGIST_EDIT_BACK:  
  3. {  
  4.             reg.show(args);  
  5.             Thread thread = new Thread(reg);  
  6.             thread.start();  
  7.             midlet.setCurrent(reg);  
  8.             break;  

UserRegist.java

  1. public void checkTimeStamp()  
  2. {  
  3.     long currentTime = System.currentTimeMillis();  
  4.     //System.out.println("1");  
  5.     if(lastCaretBlink + caretBlinkDelay < currentTime)  
  6.     {  
  7.         //System.out.println("2");  
  8.         if(editor.equals("regist_name"))  
  9.         {  
  10.             cursorBlinkOn1 =! cursorBlinkOn1;  
  11.             cursorBlinkOn2 = false;  
  12.             cursorBlinkOn3 = false;  
  13.         }  
  14.         else if(editor.equals("regist_passwd"))  
  15.         {  
  16.             cursorBlinkOn1 = false;  
  17.             cursorBlinkOn2 =! cursorBlinkOn2;  
  18.             cursorBlinkOn3 = false;  
  19.         }  
  20.         else if(editor.equals("regist_passwd_re"))  
  21.         {  
  22.             cursorBlinkOn1 = false;  
  23.             cursorBlinkOn2 = false;  
  24.             cursorBlinkOn3 =! cursorBlinkOn3;  
  25.         }  
  26.         lastCaretBlink = currentTime;  
  27.     }  
  28. }  
  29.  
  30. public void run()  
  31. {  
  32.     //System.out.println("run");  
  33.     while(true)  
  34.     {  
  35.         checkTimeStamp();  
  36.           
  37.         redraw();  
  38.         try   
  39.         {  
  40.             synchronized(this)  
  41.             {  
  42.                 //System.out.println("3");  
  43.                 wait(50L);  
  44.             }             
  45.         }  
  46.         catch(Exception e)  
  47.         {  
  48.             e.printStackTrace();  
  49.         }  
  50.               
  51.     }  

4 调用高级界面TextBox子类PopUpTextBox

调用时,将调用对象名、编辑对象名、以及编辑框参数传递给PopUpTextBox对象(一定要有,目的是保存编辑框的值,否则多次调用返回时,不同编辑框的值被刷新为空了)

UserRegist.java(KeyPressed)

  1. case KeyID.KEY_EDIT:  
  2. case KEY_NUM0:  
  3. case KEY_NUM1:  
  4. case KEY_NUM2:  
  5. case KEY_NUM3:  
  6. case KEY_NUM4:  
  7. case KEY_NUM5:  
  8. case KEY_NUM6:  
  9. case KEY_NUM7:  
  10. case KEY_NUM8:  
  11. case KEY_NUM9:  
  12. {  
  13.     //System.out.println(editor);  
  14.     Object[] args = {object_name,editor,username,passwd,passwd_re};  
  15.     controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);  
  16.     break;  

UIController.java

  1. case EventID.EVENT_USER_REGIST_EDIT:  
  2. {  
  3.      textBox.init(args);  
  4.          midlet.setCurrent(textBox);  
  5.      break;  

5 PopUpTextBox参数的接收

  1. public void init(Object[] args)  
  2. {  
  3.      object_name = ((String)args[0]!=null)?(String)args[0]:"";  
  4.      editor = ((String)args[1]!=null)?(String)args[1]:"";  
  5.      //System.out.println(object_name);  
  6.      //System.out.println(editor);  
  7.      args_t = args;  
  8.      this.setString("");  

6 PopUpTextBox返回输入法输入的值

  1. if (update) canvasText = this.getString(); 

#p#

7 PopUpTextBox输入值处理

依据调用的对象,以及编辑对象,对输入的值进行处理,传递给父对象编辑框

  1. if(object_name.equals("registScreen"))  
  2. {  
  3.         if(editor.equals("regist_name"))  
  4.         {  
  5.                 if(args_t[3]!=""||args_t[3]!=null||args_t[4]!=""||args_t[4]!=null)  
  6.                 {  
  7.                     args[0] = object_name;  
  8.                     args[1] = editor;  
  9.                     args[2] = this.canvasText;  
  10.                     args[3] = args_t[3];  
  11.                     args[4] = args_t[4];  
  12.                 }  
  13.                 controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);  
  14.     }  
  15.     else if(editor.equals("regist_passwd"))  
  16.     {  
  17.                 if(args_t[2]!=""||args_t[2]!=null||args_t[4]!=""||args_t[4]!=null)  
  18.                 {  
  19.                     args[0] = object_name;  
  20.                     args[1] = editor;  
  21.                     args[2] = args_t[2];  
  22.                     args[3] = this.canvasText;  
  23.                     args[4] = args_t[4];  
  24.                 }  
  25.                 controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);  
  26.     }  
  27.     else if(editor.equals("regist_passwd_re"))  
  28.     {  
  29.                 if(args_t[2]!=""||args_t[2]!=null||args_t[3]!=""||args_t[3]!=null)  
  30.                 {  
  31.                     args[0] = object_name;  
  32.                     args[1] = editor;  
  33.                     args[2] = args_t[2];  
  34.                     args[3] = args_t[3];  
  35.                     args[4] = this.canvasText;  
  36.                 }  
  37.                 controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);  
  38.     }  

8 输入值的显示

(1) 新建对象

  1. private TextEdit textEdit_name;  
  2. textEdit_name = new TextEdit(this);  

(2) 接受输入的参数

  1. object_name = ((String)args[0]!=null)?(String)args[0]:"";  
  2. editor = ((String)args[1]!=null)?(String)args[1]:"";  
  3. username = ((String)args[2]!=null)?(String)args[2]:"";  
  4. passwd = ((String)args[3]!=null)?(String)args[3]:"";  
  5. passwd_re = ((String)args[4]!=null)?(String)args[4]:""

(3) 光标控制,定位到编辑对象,控制编辑对象的光标闪烁(run方法)

  1. private void redraw()  
  2. {  
  3.     switch(currentlySelectedIndex)  
  4.     {  
  5.         case 0:  
  6.         {  
  7.             cursorBlinkOn2 = false;  
  8.             cursorBlinkOn3 = false;  
  9.             editor = "regist_name";  
  10.             break;  
  11.         }  
  12.         case 1:  
  13.         {  
  14.             cursorBlinkOn1 = false;  
  15.             cursorBlinkOn3 = false;  
  16.             editor = "regist_passwd";  
  17.             break;  
  18.         }  
  19.         case 2:  
  20.         {  
  21.             cursorBlinkOn1 = false;  
  22.             cursorBlinkOn2 = false;  
  23.             editor = "regist_passwd_re";  
  24.             break;  
  25.         }  
  26.         default:;  
  27.     }  
  28. //...  
  29.  

(4) 编辑框的绘制

  1. private void redraw()  
  2. {  
  3.     ...       
  4.     textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);  
  5.     textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);  
  6.     textEdit_passwd.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);  
  7.     textEdit_name.flushGraphics();  
  8. }  

实现的效果如图1所示:


图1 编辑框效果

【参考文献】

(1) http://www.developer.nokia.com/Community/Wiki/Custom_Text_Input_in_Java_ME

(2) j2me跳转到高级界面获得输入法

(3) http://www.iteye.com/topic/394113

(4) http://topic.csdn.net/t/20060906/09/5001597.html

原文链接:http://blog.csdn.net/tandesir/article/details/7544795

【系列文章】

  1. JavaMe开发:低级界面绘图之点阵字
  2. JavaMe开发:低级界面绘图之菜单
  3. JavaMe开发:也说MVC设计模式
  4. JavaMe开发:绘制可自动换行文本
  5. JavaMe开发:绘制文本框TextEdit
  6. JavaM开发:自适应滚动显示
责任编辑:林师授 来源: tandesir的博客
相关推荐

2012-05-09 10:22:44

JavaMEJava

2012-06-06 15:15:00

jQuery

2012-06-29 14:13:10

2010-01-14 11:09:35

VB.NET文本框

2010-01-18 17:37:32

VB.NET文本框处理

2009-08-25 11:03:04

c#清除文本框中内容

2009-11-03 16:20:16

VB.NET文本框

2010-01-21 15:56:31

VB.NET文本框

2013-06-17 13:47:41

WP7开发Windows Pho文本框水印控件

2012-05-09 10:58:25

JavaMEJava

2023-04-12 07:25:55

2012-05-09 10:09:18

JavaMEJava

2012-05-09 10:03:08

JavaMEJava

2021-10-19 09:31:19

Javascript 登录表单前端

2010-01-19 12:53:59

VB.NET Text

2012-05-09 09:49:54

JavaMeJava

2024-01-10 08:57:41

PyQt6Python控件

2010-06-03 14:01:26

JavaSEJavaMEiPhone

2021-04-09 11:30:50

PythonExcel代码

2009-12-17 13:46:01

Ruby表单语句
点赞
收藏

51CTO技术栈公众号