?? bankcardfield.java
字號:
package com.jmobilecore.ui;
import com.jmobilecore.ui.core.TextField;
import com.jmobilecore.ui.core.TextComponent;
/**
* This abstract class displays formatted textfield for entering bank card
* information (like VISA/MasterCard/AMEX).
* Blocks of digits are separated by space.
*
* @author Igor Shevlyakov - initial implementation
* @author Greg Gridin - redesign
*/
abstract public class BankCardField extends TextField {
/**
* Constructs a new <code>BankCardField</code> field.
*/
public BankCardField() {
super(-1, TextComponent.C_NUMERIC);
composer = initComposer();
composer.setCaretPosition(0);
};
/**
* Abstract method for initiating bank card field composer.
* This methods should be defined in inherited classes
*/
abstract protected CustomFieldComposer initComposer();
/**
* Constructs a new <code>BankCardField</code> object of type
* <code>style</code>
* with the specified value.
*
* @param cardNumber the card number, unformatted, no spaces or dashes
*/
public BankCardField(String cardNumber) {
this();
setText(cardNumber);
}
/**
* Get formatted representation of <code>BankCardField</code> value
* or <code>null</code> if <code>BankCardField</code> object value is set to null or incomplete
*
* @return the <code>String</code> representing bank card number
*/
public String getFormattedText() {
if (isValid()) {
return String.valueOf(getFormattedText(false));
}
return null;
}
/**
* Sets the <code>BankCardField</code> field to specified value
*
* @param cardNumber the card number, unformatted, no spaces or dashes
*/
public void setText(String cardNumber) {
if (validate(cardNumber)) {
composer.setText(cardNumber);
}
}
/**
* Tests bank card field value for correctness
*
* @return <code>true</code> if the bank card field is valid
* <code>false</code> otherwise
*/
public boolean isValid() {
return ((CustomFieldComposer) composer).isComplete();
}
/**
* Tests if the specified card valid
*
* @param cardNumber bank card number without formatting (spaces, dashes etc.)
* @return <code>true</code> if the bank card field is valid
* <code>false</code> otherwise
*/
public boolean validate(String cardNumber) {
if (cardNumber != null) {
final int FIELD_LEN = ((CustomFieldComposer) composer).getMaxSize();
if (cardNumber.length() == FIELD_LEN) {
char curChar;
for (int i = 0; i < FIELD_LEN; i++) {
curChar = cardNumber.charAt(i);
if (!(Character.isDigit(curChar))) return false;
}
return true;
}
}
return false;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -