• EditText输入手机号自动带空格


    xml:

    <EditText
    android:id="@+id/edit_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号"
    android:inputType="number"
    android:maxLength="13"/>

    Activity:

    mEditText = (EditText) findViewById(R.id.edit_main);
    mEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
    if (charSequence == null || charSequence.length() == 0)
    return;
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < charSequence.length(); i++) {
    if (i != 3 && i != 8 && charSequence.charAt(i) == ' ') {
    continue;
    } else {
    stringBuilder.append(charSequence.charAt(i));
    if ((stringBuilder.length() == 4 || stringBuilder.length() == 9)
    && stringBuilder.charAt(stringBuilder.length() - 1) != ' ') {
    stringBuilder.insert(stringBuilder.length() - 1, ' ');
    }
    }
    }
    if (!stringBuilder.toString().equals(charSequence.toString())) {
    int index = start + 1;
    if (stringBuilder.charAt(start) == ' ') {
    if (before == 0) {
    index++;
    } else {
    index--;
    }
    } else {
    if (before == 1) {
    index--;
    }
    }
    mEditText.setText(stringBuilder.toString());
    mEditText.setSelection(index);
    }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
    });
  • 相关阅读:
    赵炯博士《Linux内核完全注释》
    0.11内核rd_load@ramdisk.c中memcpy函数好像有bug
    Can't find kernel text map area from kcore
    Could not allocate 40960 bytes percpu data
    镜像
    H3C S6800交换机 BCM shell命令
    Bean的作用域
    Bean之间的关系
    Bean的自动装配
    Java8 Hash改进/内存改进
  • 原文地址:https://www.cnblogs.com/Talon-lly/p/6397054.html
Copyright © 2020-2023  润新知