PhoneEditText.java 3.71 KB
Newer Older
1
package com.ydl.ydlcommon.view;
konghaorui committed
2 3 4 5 6 7 8 9 10 11 12 13 14

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;

/**
 * Created by jiucheng on 2018/5/15.
 * 功能:手机号输入框--自动添加空格  131 1234 5678 {@link #setPhoneText(String phone)}
 * 获取输入框内容时,自动取消空格 {@link #getTextContent()}
 */

YKai committed
15
public class PhoneEditText extends androidx.appcompat.widget.AppCompatEditText {
konghaorui committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    public PhoneEditText(Context context) {
        super(context);
        init(context);
    }

    public PhoneEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PhoneEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("tetetset", s + "--" + String.valueOf(start) + "--" +  String.valueOf(before) + "--" +  String.valueOf(count));
                if (s == null || s.length() == 0) return;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < s.length(); i++) {
                    if (i != 3 && i != 8 && s.charAt(i) == ' ') {
                        continue;
                    } else {
                        sb.append(s.charAt(i));
                        if ((sb.length() == 4 || sb.length() == 9) && sb.charAt(sb.length() - 1) != ' ') {
                            sb.insert(sb.length() - 1, ' ');
                        }
                    }
                }
                if (!sb.toString().equals(s.toString())) {
                    int index = start + 1;
                    if (start < sb.length() - 1 && sb.charAt(start) == ' ') {
                        if (before == 0) {
                            index++;
                        } else {
                            index--;
                        }
                    } else {
                        if (before == 1) {
                            index--;
                        }
                    }
                    setText(sb.toString());
67 68
                    if (index > sb.length())
                        index = sb.length();
konghaorui committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
                    setSelection(index);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (listener != null) {
                    listener.setOnTextChangedListener(s);
                }
            }
        });
    }

    OnAfterTextChangedListener listener;

    public void setListener(OnAfterTextChangedListener listener) {
        this.listener = listener;
    }

    public interface OnAfterTextChangedListener {
        void setOnTextChangedListener(Editable s);
    }

    /**
     * 获取输入框内容,输入框中的空格
     *
     * @return 不带空格的String
     */
    public String getTextContent() {
        String content = getText().toString().trim();
        return content.replace(" ", "");
    }

    /**
     * 将电话号码填入输入框,并增加空客
     *
     * @param phone 电话号码
     */
    public void setPhoneText(String phone) {
        if (phone.length() == 11) {
            StringBuffer sb = new StringBuffer(phone);
            sb.insert(7, " ");
            sb.insert(3, " ");
            setText(sb.toString());
            setSelection(sb.toString().length());
        }
    }
}