📄 postalcodefield.java
字号:
package com.jmobilecore.ui;
import com.jmobilecore.ui.core.TextField;
import com.jmobilecore.ui.core.TextComponent;
/**
* This class displays formatted textfield for entering postal code information
* Format of the postal code is specified by user.
*
* @author Igor Shevlyakov - initial implementation
* @author Greg Gridin - redesign
*/
public class PostalCodeField extends TextField {
/**
* The 5 digits postal code, example 12345
*/
public static final byte XXXXX = 1;
/**
* The 5 digits postal code with extension, example 12345-6789
*/
public static final byte XXXXX_XXXX = 2;
/**
* The 6 digits postal code, example 123456
*/
public static final byte XXXXXX = 3;
/**
* The current PostalCodeField formatting style.
*/
final public byte STYLE;
/**
* Constructs a new <code>PostalCodeField</code> object.
*/
public PostalCodeField() {
this(XXXXX);
}
/**
* Constructs a new <code>PostalCodeField</code> object of specified style.
*
* @param style the postal code style
*/
public PostalCodeField(byte style) {
super(-1, TextComponent.C_NUMERIC);
STYLE = style;
composer = initComposer(style);
composer.setCaretPosition(0);
};
/**
* Constructs a new <code>PostalCodeField</code> object
* of the specified style and the specified value.
*
* @param style the postal code style
* @param postalCode the postal code, digits only no spaces or dashes
*/
public PostalCodeField(byte style, String postalCode) {
this(style);
setText(postalCode);
}
/**
* Initialize postal code field composer
*
* @return <code>CustomFieldComposer</code> for postal code field
*/
protected CustomFieldComposer initComposer(int style) {
TextBlockComposer[] textBlocks = null;
if (STYLE == XXXXX) {
textBlocks = new TextBlockComposer[1];
textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 5);
}
if (STYLE == XXXXXX) {
textBlocks = new TextBlockComposer[1];
textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 6);
}
if (STYLE == XXXXX_XXXX) {
textBlocks = new TextBlockComposer[2];
textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 5);
textBlocks[1] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 4);
}
return new CustomFieldComposer(textBlocks) {
public void setText(String text) {
if (text == null) return;
if (STYLE == XXXXX) {
textBlocks[0].setText(text.substring(0, 5));
}
if (STYLE == XXXXXX) {
textBlocks[0].setText(text.substring(0, 6));
}
if (STYLE == XXXXX_XXXX) {
textBlocks[0].setText(text.substring(0, 5));
textBlocks[1].setText(text.substring(5, 9));
}
setCaretPosition(0);
}
};
}
/**
* Get formatted according to <code>STYLE</code> phone number
* or <code>null</code> if <code>PhoneNumberField</code> object value is set to null or incomplete
*
* @return the <code>String</code> representing phone number
*/
public String getFormattedText() {
return composer.getText();
}
protected char[] getFormattedText(boolean calcCursorOffset) {
char unformatted[] = composer.getText().toCharArray();;
int fmtCaretPos=0;
char formattedText[];
if (STYLE == XXXXX || STYLE == XXXXXX) {
formattedText = unformatted;
fmtCaretPos = composer.getCaretPosition();
currentLength = formattedText.length;
} else {
currentLength = unformatted.length+1;
formattedText = new char[currentLength];
TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
System.arraycopy(blocks[0].getChars(), 0, formattedText, 0, 5);
formattedText[5]='-';
System.arraycopy(blocks[1].getChars(), 0, formattedText, 6, 4);
}
if (calcCursorOffset) {
calculatedCaretPosition = fmtCaretPos;
calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
}
return formattedText;
}
/**
* Sets the <code>PostalCodeField</code> object to specified value
*
* @param postalCode the postal code, digits only no spaces or dashes
*/
public void setText(String postalCode) {
if (validate(postalCode)) {
composer.setText(postalCode);
}
}
/**
* Tests postal code field value for correctness
*
* @return <code>true</code> if the postal code field is valid
* <code>false</code> otherwise
*/
public boolean isValid() {
return ((CustomFieldComposer) composer).isComplete();
}
/**
* Tests if the specified postal code valid
*
* @param postalCode postal code number without formatting (spaces, dashes etc.)
* @return <code>true</code> if the postal code field is valid
* <code>false</code> otherwise
*/
public boolean validate(String postalCode) {
if (postalCode != null) {
final int FIELD_LEN = ((CustomFieldComposer) composer).getMaxSize();
if (postalCode.length() == FIELD_LEN) {
char curChar;
for (int i = 0; i < FIELD_LEN; i++) {
curChar = postalCode.charAt(i);
if (!(Character.isDigit(curChar))) return false;
}
return true;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -