欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android手机号码输入框(满11位自动跳到下个输入框)实例代码

时间:2020-10-25 10:00:18|栏目:Android代码|点击:

废话不多说了,直接给大家贴代码了,具体代码如下所示:

package com.jixiong.teen.view;
import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
/**
 * Created by christy on 16/12/22.
 */
public class MoblieEditText extends EditText {
  public MoblieEditText(Context context) {
    super(context);
    this.addTextChangedListener(new MoblieWatcher());
  }
  public MoblieEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.addTextChangedListener(new MoblieWatcher());
  }
  public MoblieEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.addTextChangedListener(new MoblieWatcher());
  }
  class MoblieWatcher implements TextWatcher {
    int beforeTextLength = 0;
    int onTextLength = 0;
    boolean isChanged = false;
    int location = 0;// 记录光标的位置
    private char[] tempChar;
    private final 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 = 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)) {
            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;
        }
        setText(str);
        Editable etable = getText();
        Selection.setSelection(etable, location);
        isChanged = false;
      }
    }
  }
}

使用;;

直接在布局中引用

<com.jixiong.teen.view.MoblieEditText
  android:id="@+id/etUserNums"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@null"
  android:hint="@string/user_name"
  android:inputType="number"
  android:maxLines="1"
  android:paddingLeft="@dimen/margin_twenty"
  android:singleLine="true"
  android:textColorHint="@color/hint_color"
  android:textSize="@dimen/sp_14" />

然后再activity中初始化

etUserNums.addTextChangedListener(new TeenEmptyWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
  }
  @Override
  public void afterTextChanged(Editable s) {
    if (s != null && s.length() == 13) {
      if (etUserNums.isFocused()) {
        etUserNums.clearFocus();
        etUserPwd.requestFocus();
      }
    }
  }
});

总结

上一篇:深入Android 五大布局对象的应用

栏    目:Android代码

下一篇:Android TextSwitcher文本切换器和ViewFlipper使用详解

本文标题:Android手机号码输入框(满11位自动跳到下个输入框)实例代码

本文地址:http://www.codeinn.net/misctech/15702.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有