2010-01-05 3 views
1

내가 입력 한 첫 번째 문자가 오른쪽으로 대부분의 위치를가는 기본 편집 필드를 원하는 ... 이런 식으로 뭔가를 ..BasicEditField 정의는

    2 

       23 

       234 

이 하나가 ... 방법이 작업을 수행하는

을 말해 줄 수

답변

2

여기 하나의 방법 구현해야한다 :이 도움이 될

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

줄 바꿈을 방지하려면 super (TextField.NO_NEWLINE); –

+0

@AlexanderFarber Enter 키를 사용할 때 초점을 아래로 이동시키는 이점이 있습니다. –

+0

@clafonta 직접 필드 크기를 처리하는 대신'BasicEditField.setMaxSize (int maxSize)'를 사용합니다. 그러나 이것은 스타일의 문제 일뿐입니다. 나는 다른 독자들에게 완전성을 말했습니다. –

0

스타일 사용 Field.FIELD_RIGHT.

+0

시도해 보셨습니까? 나는 필드 자체를 오른쪽에 넣는 것이지 내부의 문자는 넣지 않을 것이라고 생각한다. –

+0

아니요, 편집 할 때까지하지 않았습니다. 레이블 작업 :) –

0

확신에는이 작업을 수행하는 방법에 내장이 없다는 것을, 당신은 Gona을 자신의 텍스트 상자에게 그것을 할

2

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); 
    } 
}; 
관련 문제