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 ...
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 ...
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;
}
}
Utilisez le style Field.FIELD_RIGHT.
Avez-vous essayé? Je suppose que cela ne ferait que mettre le champ lui-même à droite, pas les caractères à l'intérieur. –
Non, sur les modifications je n'ai pas. Fonctionne sur les étiquettes :)) –
À 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
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);
}
};
Pour éviter les sauts de ligne, vous pouvez simplement utiliser super (TextField.NO_NEWLINE); –
@AlexanderFarber Ceci a le bon avantage de réduire la focale lorsque Enter est utilisé. –
@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. –