美文网首页
Android 完美实现手机号344格式化效果

Android 完美实现手机号344格式化效果

作者: IT一书生 | 来源:发表于2018-03-19 09:02 被阅读1092次

一、前言:

在展示手机号码的时候,会遇到手机号按照344格式效果,这种效果的实现遇到过两次了,也踩过了许多的坑,在这里记录一下一个完美实现这种效果的方式。输入、插入、删除等光标位置停留比较好的交互效果。

二、效果:

捕获.PNG

三、代码:

public class ZpPhoneEditText extends AppCompatEditText implements TextWatcher {  
  
    // 特殊下标位置  
    private static final int PHONE_INDEX_3 = 3;  
    private static final int PHONE_INDEX_4 = 4;  
    private static final int PHONE_INDEX_8 = 8;  
    private static final int PHONE_INDEX_9 = 9;  
  
    public ZpPhoneEditText(Context context) {  
        super(context);  
        initView();
    }  
  
    public ZpPhoneEditText(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        initView();
    }  
  
    public ZpPhoneEditText(Context context, AttributeSet attrs, int defStyleAttr) {  
        super(context, attrs, defStyleAttr);  
        initView();
    }  

    private void initView() {
        setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, 
                                                Spanned spanned, int dstart, int dend) {
                        if (" ".equals(source.toString()) || source.toString().contentEquals("\n") || dstart == 13) {
                            return "";
                        } else {
                            return null;
                        }
                    }
                }
        });
    }
  
    @Override  
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  
    }  
  
    @Override  
    public void onTextChanged(CharSequence s, int start, int before, int count) {  
        super.onTextChanged(s, start, before, count);  
        if (s == null || s.length() == 0) {  
            return;  
        }  
        StringBuilder sb = new StringBuilder();  
        for (int i = 0; i < s.length(); i++) {  
            if (i != PHONE_INDEX_3 && i != PHONE_INDEX_8 && s.charAt(i) == ' ') {  
                continue;  
            } else {  
                sb.append(s.charAt(i));  
                if ((sb.length() == PHONE_INDEX_4 || sb.length() == PHONE_INDEX_9) && sb.charAt(sb.length() - 1) != ' ') {  
                    sb.insert(sb.length() - 1, ' ');  
                }  
            }  
        }  
        if (!sb.toString().equals(s.toString())) {  
            int index = start + 1;  
            if (sb.charAt(start) == ' ') {  
                if (before == 0) {  
                    index++;  
                } else {  
                    index--;  
                }  
            } else {  
                if (before == 1) {  
                    index--;  
                }  
            }  
  
            setText(sb.toString());  
            setSelection(index);  
        }  
    }  
  
    @Override  
    public void afterTextChanged(Editable s) {  
  
    }  
  
    // 获得不包含空格的手机号  
    public String getPhoneText() {  
        String str = getText().toString();  
        return replaceBlank(str);  
    }  
  
    private String replaceBlank(String str) {  
        String dest = "";  
        if (str != null) {  
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
            Matcher m = p.matcher(str);  
            if (m.find()) {  
                dest = m.replaceAll("");  
            }  
        }  
        return dest;  
    }  
}  

布局XML

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:gravity="center"  
    android:orientation="vertical">  
  
    <com.example.zpdemo.widget.ZpPhoneEditText  
        android:id="@+id/et_phone"  
        android:layout_width="300dp"  
        android:layout_height="wrap_content"  
        android:hint="格式化手机号344"  
        android:inputType="phone"  
        android:maxLength="13"/>  
  
    <Button  
        android:id="@+id/btn_phone"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="20dp"  
        android:textColor="#f00"  
        android:textSize="16sp"  
        android:text="获得手机号"/>  
  
</LinearLayout>  

应用

private ZpPhoneEditText etPhone;  
    private Button btnPhone;  
  
    private void initView() {  
        etPhone = (ZpPhoneEditText) findViewById(R.id.et_phone);  
        btnPhone = (Button) findViewById(R.id.btn_phone);  
        btnPhone.setOnClickListener(new View.OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                btnPhone.setText(etPhone.getPhoneText());  
            }  
     });  
} 

效果杠杠的,代码比较完整,亲自尝试一下比较好。。。

相关文章

网友评论

      本文标题:Android 完美实现手机号344格式化效果

      本文链接:https://www.haomeiwen.com/subject/ypanfftx.html