2010-01-05 7 views
1

Je veux un champ d'édition de base où le premier caractère saisi va à droite position la plus ... quelque chose comme ça ..BasicEditField Personnalisation

    2 

       23 

       234 

peut me dire comment faire ...

Répondre

2

Voici une façon de le faire:

import net.rim.device.api.system.Characters; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.DrawStyle; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.XYRect; 
import net.rim.device.api.ui.component.BasicEditField; 

public class RightInsertTextField extends BasicEditField { 
    private String lastGoodTxtInputEntry = null; 
    private int maxCharInputLength = 20; 
    private Field nextFieldForFocus = null; 

/** 
* Basic constructor. 
* 
* @param defaultValue 
*/ 
public RightInsertTextField(String defaultValue) { 
    super("", defaultValue); 
} 

public void paint(Graphics g) { 
    String txt = this.getText(); 
    // 1. (Optional) keeping a check on input length can help 
    // minimize custom code needed to handle wrap-around text. 
    if (txt.length() > this.getMaxCharInputLength() && this.getMaxCharInputLength() > 0) { 
     txt = this.lastGoodTxtInputEntry; 
     this.setText(txt); 
    } else { 
     this.lastGoodTxtInputEntry = txt; 
    } 

    // 2. get rid of the default cursor painting by coloring over it with 
    // the same color as the background 
    XYRect xy = g.getClippingRect(); 
    g.setBackgroundColor(Color.WHITE); 
    g.fillRect(xy.x, xy.y, xy.width, xy.height); 
    g.clear(); 

    // 3. Align text to the right. 

    g.setColor(Color.BLACK); 
    g.drawText(txt, 0, 0, (DrawStyle.RIGHT + DrawStyle.ELLIPSIS), getWidth()); 

} 

/** 
* Look out for 'ENTER' being pressed. 
*/ 
public boolean keyChar(char key, int status, int time) { 
    // Prevent new lines in input field. 
    if (Characters.ENTER == key) { 
     if (this.nextFieldForFocus != null) { 
      this.nextFieldForFocus.setFocus(); 
     } 
     return true; 
    } else { 
     return super.keyChar(key, status, time); 
    } 
} 

public void setMaxCharInputLength(int maxCharInputLength) { 
    this.maxCharInputLength = maxCharInputLength; 
} 

public int getMaxCharInputLength() { 
    return maxCharInputLength; 
} 

/** 
* @param nextFieldForFocus 
*   the nextFieldForFocus to set if 'ENTER' is pressed. 
*/ 
public void setNextFieldForFocus(Field nextFieldForFocus) { 
    this.nextFieldForFocus = nextFieldForFocus; 
} 

} 
+0

Pour éviter les sauts de ligne, vous pouvez simplement utiliser super (TextField.NO_NEWLINE); –

+0

@AlexanderFarber Ceci a le bon avantage de réduire la focale lorsque Enter est utilisé. –

+0

@clafonta J'utiliserais 'BasicEditField.setMaxSize (int maxSize)' pour gérer la taille maximale des champs, plutôt que de devoir le faire vous-même. Cependant, ce n'est qu'une question de style - je le mentionne juste pour être complet aux autres lecteurs. –

0

Utilisez le style Field.FIELD_RIGHT.

+0

Avez-vous essayé? Je suppose que cela ne ferait que mettre le champ lui-même à droite, pas les caractères à l'intérieur. –

+0

Non, sur les modifications je n'ai pas. Fonctionne sur les étiquettes :)) –

0

À peu près sûr qu'il n'y a pas construit de manière à ce faire, vous gona devez implémenter votre propre zone de texte

2

Cela aiderait

editField = new EditField("", "", maxChars, EditField.NO_NEWLINE | EditField.NON_SPELLCHECKABLE){ 
    private String text = ""; 
    protected boolean keyChar(char key, int status, int time){ 
     switch (key){ 
      case Characters.BACKSPACE:{ 
       try { 
        text = text.substring(0,text.length()-1); 
        invalidate(); 
       } catch (IndexOutOfBoundsException e) {} 
       return true; 
      } 
     } 
     text = text + key; 
     invalidate(); 
     return true; 
    } 
    protected void paint(Graphics graphics) { 
     graphics.drawText(text,0, 0, DrawStyle.RIGHT, width - 10); 
     super.paint(graphics); 
    } 
};