visacardfield.java

来自「j2me下的1套UI框架.包含j2me开发中会应用的各种低级组件」· Java 代码 · 共 56 行

JAVA
56
字号
package com.jmobilecore.ui;

/**
 * This class displays text field for entering bank card AmericanExpress
 * card information (format is: 1234 5678 1234 5678)
 *
 * @author Greg Gridin
 */
public class VisaCardField extends BankCardField {

    /**
     * Implementation of abstract <code>BankCardField</code> method
     *
     * @return <code>CustomFieldComposer</code> for Visa/MasterCard card
     */
    protected CustomFieldComposer initComposer() {
        TextBlockComposer[] textBlocks = new TextBlockComposer[4];
        textBlocks[0] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 4);
        textBlocks[1] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 4);
        textBlocks[2] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 4);
        textBlocks[3] = new DigitalBlockComposer(DigitalBlockComposer.FIXED_SIZE, 4);

        return new CustomFieldComposer(textBlocks) {
            public void setText(String text) {

                if (text == null || text.length() != getMaxSize()) return;
                textBlocks[0].setText(text.substring(0, 4));
                textBlocks[1].setText(text.substring(4, 8));
                textBlocks[2].setText(text.substring(8, 12));
                textBlocks[3].setText(text.substring(12, 16));
                setCaretPosition(0);
            }
        };
    }

    protected char[] getFormattedText(boolean calcCursorOffset) {

        TextBlockComposer blocks[] = ((CustomFieldComposer) composer).textBlocks;
        currentLength = 19;
        char[] formattedText = new char[currentLength];
        System.arraycopy(blocks[0].getChars(), 0, formattedText, 0, 4);
        formattedText[4] = ' ';
        System.arraycopy(blocks[1].getChars(), 0, formattedText, 5, 4);
        formattedText[9] = ' ';
        System.arraycopy(blocks[2].getChars(), 0, formattedText, 10, 4);
        formattedText[14] = ' ';
        System.arraycopy(blocks[3].getChars(), 0, formattedText, 15, 4);
        if (calcCursorOffset) {
            calculatedCaretPosition = composer.getCaretPosition() + ((CustomFieldComposer)composer).getCurrentBlock();
            calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
        }
        return formattedText;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?