1. 程式人生 > >Android EditText輸入框按銀行賬號格式輸入(4 4 4 4 3)

Android EditText輸入框按銀行賬號格式輸入(4 4 4 4 3)

我們在做輸入框輸入的時候總會遇到各種各樣的格式輸入要求,下面來給大家介紹一個按銀行賬號3 4 4 4 4格式的輸入(5222 2222 2222 2222 222)。就是輸入完前三個數加一個空格,之後每輸入完四個數加一個空格,並控制只能輸入銀行賬號的字元數。下面來看程式碼:

xml佈局檔案:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/color_white"
    android
:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_weight="1" android:text="@string/account_withdraw_account_number"
android:textColor="@color/color_333333" android:textSize="@dimen/font_size_18" android:textStyle="bold" /> <EditText android:id="@+id/activity_bank_number_et" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight=
"15dp" android:layout_weight="2" android:background="@null" android:hint="@string/account_withdraw_input_bank_number" android:inputType="numberSigned" android:maxLength="23" android:textColorHint="@color/color_d5d5d5" /> </LinearLayout>
   

activity java程式碼:

//設定賬號輸入框的輸入格式為:3 4 4 4 4
private void iniSetBankNumberEtText(final EditText mEditText) {
    mEditText.addTextChangedListener(new TextWatcher() {
        int beforeTextLength = 0;
        int onTextLength = 0;
        boolean isChanged = false;
        int location = 0;// 記錄游標的位置
        private char[] tempChar;
        private StringBuffer buffer = new StringBuffer();
        int konggeNumberB = 0;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            beforeTextLength = s.length();
            if (buffer.length() > 0) {
                buffer.delete(0, buffer.length());
            }
            konggeNumberB = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == ' ') {
                    konggeNumberB++;
                }
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            onTextLength = s.length();
            buffer.append(s.toString());
            if (onTextLength == beforeTextLength || onTextLength <= 3
                    || isChanged) {
                isChanged = false;
                return;
            }
            isChanged = true;
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (isChanged) {
                location = mEditText.getSelectionEnd();
                int index = 0;
                while (index < buffer.length()) {
                    if (buffer.charAt(index) == ' ') {
                        buffer.deleteCharAt(index);
                    } else {
                        index++;
                    }
                }
                index = 0;
                int konggeNumberC = 0;
                while (index < buffer.length()) {
                    if ((index == 3 || index == 8 || index == 13 || index == 18)) {
                        buffer.insert(index, ' ');
                        konggeNumberC++;
                    }
                    index++;
                }
                if (konggeNumberC > konggeNumberB) {
                    location += (konggeNumberC - konggeNumberB);
                }
                tempChar = new char[buffer.length()];
                buffer.getChars(0, buffer.length(), tempChar, 0);
                String str = buffer.toString();
                if (location > str.length()) {
                    location = str.length();
                } else if (location < 0) {
                    location = 0;
                }
                mEditText.setText(str);
                Editable etable = mEditText.getText();
                Selection.setSelection(etable, location);
                isChanged = false;
            }
        }
    });
 這樣就實現的這個功能 ,原創作品,歡迎轉載。