欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-09 19:52:33
废话不多说了,直接给大家贴代码了,具体代码如下所示: package com.jixiong.teen.view; import android.content...

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

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手机号码输入框(满11位自动跳到下个输入框)实例代码,希望对大家有所帮助