【Descripción del problema】
Textedit es un editor de texto dibujado con GameCanvas. Este documento combina ejemplos para proporcionar métodos de implementación.
【principio】
1 Use gráficos y gamecanvas para dibujar cuadros de texto y cursores.
2 Cuando se detecta un evento de entrada, salte a la interfaz avanzada -> TextBox. Complete la entrada a través del método de entrada de llamadas del sistema.
3 Devuelva el valor ingresado por TextBox al objeto TextEdit.
【Patrón de diseño】
Este proceso es un poco similar al modo decorativo.
【Lista de código】
Textedit.javapackage com.token.view.components; ; ) { / /System.out.println("draw "); ); , ancho+margen, altura+margen); y+1, ancho -1, altura -1); +ft.StringWidth (texto)+relleno, y+relleno, 1, ft.getheight (), cursorblinkon); , int x, int y, int width, int altura, boolean cursorblinkon) {if (cursorblinkon) {ft = font.getFont (font.face_proportional, font.style_lain, font.size_medium); .SetColor (0x0,0x0, 0x0); ; Canvastext = ""; MaxSize, INT Restricciones) {Super (Título, Texto, MaxSize, Restricciones); "Cancelar", Command.Cancel, 1); String) Args [0]! = NULL)? (String) Args [0]: ""; System.out.println (Object_Name); getString (); | args_t [3]! = null || :::::::::::::::::: para::: ::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::: no ]! = NULL) {args [0] = Object_Name; controlador.handleevent (uicontroller.eventid.event_user_regist_edit_back, args); [4]! = "" || args_t [4]! = NULL) {args [0] = Object_Name; ; Args [4] = Args_t [4]; || args_t [2]! = null || args_t [3]! = "" || args_t [3]! = null) {args [0] = object_name; [2]; CommandListener {public void commandAction (comando de comando, displayable disp) {if (command == okcommand) {closet extbox (true); .Token.view; .*; Título de la cadena privada; y; Nombre de usuario; UserRegist (UICONTROLLER CONTROLESS) menú = nuevo (this); nuevo TextEdit (this); [0]: ""; ) args [2]: ""; (String) Args [4]: ""; ")) {cursorblinkon1 = false; cursorblinkon2 = true; cursorblinkon3 = false; actualmente selectedIndex = 1;} else if (editor.equals (" regist_passwd_re ")) {cursorblinkon1 = false; cursorblinkon2 = false; cursorblinkon3 = true; actualmente selectedx = 2; } //System.out.println(Object_name); gráficos); 0: {cursorBlinkon2 = false; cursorblinkon2 = false; _y, cu rsorblinkon2); topado .Style_Bold, Font.Size_Large); ); } textEit_name_x = 5; = StringDealMethod.Format (Info, Width-10, ft); (info_wrap2 [i], 5, (i+info_wrap1.length) * ft.getheight ()+textedit_name.height+margen+40, gráfico.top | gráfico.left); +info_wrap2 .length) * ft.getheight ()+textedit_name.height+margin+40; ] = StringDealMethod.Format (Info, Width-10, ft); Drawstring (info_wrap3 [i], 5, (i+info_wrap1.length+info_wrap2.length) * ft.getheight ()+textedit_name.height+textedit_passwd.height+2 * margen+40, graphics.top | gráfico.tft) ; , passwd_re, textedit_passwd_re_x, textedit_passwd_re_y, cursorblinkon3); = System.CurrentTimemillis (); ")) {cursorblinkon1 =! cursorblinkon1; cursorblinkon2 = false; cursorblinkon3 = false;} else if (editor.equals (" regist_passwd ")) {cursorBlinkon1 = false; cursorblin kon2 =! cursorblinkon2; cursorblinkon3 = false;} el más if (editor (editor (editor (Editor .equals ("regist_passwd_re") {cursorblinkon1 = false; (true) {checktimeStamp (); ; ! = "" && passwd! = "" && passwd_re! = "") {if (passwd.equals (passwd_re)) {userRecord.db_deleteallrecord (); UserDataItem (1, (nombre de usuario +"," +passwd) .getBytes ()); ac tiveScreen", null, update}; controller .handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_TOKEN_SCREEN,args); } } break; } case KeyID.KEY_EDIT: case KEY_NUM0: case KEY_NUM1: c ase KEY_NUM2: case KEY_NUM3: case KEY_NUM4: case KEY_NUM5 : Case Key_num6: Case Key_num7: Case Key_num8: Case Key_num9: {//system.out.println(Editor); .Event_user_regist_edit, args); {Redraw ();
【analizar】
1 Dibujo de cuadros de texto (Textedit.java)
Debe aprobar los objetos de GameCanvas y gráficos para implementar el dibujo, quién usa la estrategia y quién pasa este parámetro. Además, la esquina superior izquierda coordenada (x, y) del cuadro de texto debajo del lecho y la variable cursorblinkon que controla el parpadeo del cursor.
public void DrawTextBox (Canvas de Gamecanvas, gráficos gráficos, texto de cadena, int x, int y, boolean cursorblinkon) {//system.out.println("draw "); . RELACIÓN GRAPÍA; ancho, altura); , x+relleno, y+relleno, gráfico.top | gráfico.left); .flushgraphics (x, y, ancho, altura);
2 Dibuja el cursor (textedit.java)
public void DrawCursor (Graphics Graphics, Int X, Int Y, Int Width, int Height, Boolean Cursorblinkon) {if (cursorblinkon) {ft = font.getFont (Font.fac e_proportional, font.style_lain, font.size_medium); setFont (ft);
3 Implementar el flasheo del cursor
La implementación del flasheo del cursor requiere roscado.
UICONTROLLER () {Long CurrentTime = System.CurrentTimemillis (); .equals ("regist_name") {cursorblinkon1 =! } else if (editor.equals ("regist_passwd_re") {cursorblinkon1 = false; ejecutar "); while (true) {checktimeStamp (); redraw (); try {sincronizado (this) {//system.out.println("3"); espera (50l);}} capt (excepción e) { e.
4 Llame a la subclase de textbox de interfaz avanzada PopupTextBox
Al llamar, el nombre del objeto de llamada, editar el nombre del objeto y los parámetros del cuadro de edición se pasan al objeto PopupTextBox (debe tenerlo, el propósito es guardar el valor del cuadro de edición. De lo contrario, cuando se devuelven múltiples llamadas, los valores de diferentes cuadros de edición se actualizarán para vaciar.
UserRegist.java (KeyPressed) Case KeyID.Key_Edit: 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: {//system.out.printlnnnnnnnnnnnnnnin (editor); Args);
5 recepción de parámetros de PopupTextBox
public void init (objeto [] args) {object_name = (((string) args [0]! = NULL)? (String) args [0]: ""; ? (String) Args [1]: "";
6 PopupTextBox Devuelve la entrada de valor del método de entrada
if (update) CanVastext = this.getString ();
7 Procesamiento de valor de entrada de PopupTextBox
Procese el valor de entrada de acuerdo con el objeto llamado y el objeto Editar, y pasarlo al cuadro de edición del objeto principal.
if (object_name.equals ("RegistsCreen")) {if (editor.equals ("regist_name")) {if (args_t [3]! = "" || args_t [3]! = null || args_t [4]! = "|| args_t [4]! = NULL) {args [0] = object_name; ] = args_t [4];} controlador.handleevent (uicontroller.eventid.event_user_regist_edit_back, args); ]! = nulo || args_t [4]! = "" || args_t [4]! = [3] = this.canVastext; 2]! = "" || args_t [2]! = Null || args_t [3]! = "" || args_t [3]! = Null) {args [0] = object_name; args [2] = args_t [2];
8 Visualización de valores de entrada
(1) Crear un nuevo objeto
privado textedit textedit_name;
(2) aceptar parámetros de entrada
object_name = (((string) args [0]! = null)? (string) args [0]: ""; "; username = (((string) args [2]! = null)? (string) args [2]:" "; passwd = ((string) args [3]! = null)? (string) args [3] : "";
(3) Control del cursor, posicionamiento del objeto de edición y controlar el flasheo del cursor del objeto de edición (método Ejecutar)
Void Redraw () {Switch (Actualmente, INDEX) Break;
(4) Dibujo del cuadro de edición
private void redraw () {... textEdit_name.drawTextBox (this, gráficos, nombre de usuario, textedit_name_x, textedit_name_y, cursorblinkon1) TextBox (esto, Graphics, passwd_re, textedit_passwd_re_x, textedit_passwd_re_y, cursorblinkon3);
El efecto logrado se muestra en la Figura 1: