美国 android手机号码,格式编辑文本为美国电话号码1(xxx)-xxxx你在android中键入?...

似乎您需要将TextChangedListener添加到您的编辑中并在其中进行准备处理。 下面是开头插入+7的小例子(实际上,对 - 中间的逻辑保持不变,仅另一个字符串所需的操作):

/** Helper to control input phone number */

static class PrefixEntryWatcher implements TextWatcher {

/** flag to avoid re-enter in {@link PhoneEntryWatcher#afterTextChanged(Editable)}*/

private boolean isInAfterTextChanged = false;

/** Prefix to insert */

private final String prefix;

/** Prefix to insert length */

private final int prefixLength;

/** Weak reference to parent text edit */

private final WeakReference parentEdit;

/**

* Default constructor

*

* @param prefix to be used for prefix

*/

PrefixEntryWatcher(final String prefix, final EditText parentEdit) {

this.prefix = prefix;

this.prefixLength = (prefix == null ? 0 : prefix.length());

this.parentEdit = new WeakReference(parentEdit);

}

@Override

public synchronized void afterTextChanged(final Editable text) {

if (!this.isInAfterTextChanged) {

this.isInAfterTextChanged = true;

if (text.length() <= this.prefixLength) {

text.clear();

text.insert(0, this.prefix);

final EditText parent = this.parentEdit.get();

if (null != parent) {

parent.setSelection(this.prefixLength);

}

}

else {

if (!this.prefix.equals(text

.subSequence(0, this.prefixLength).toString())) {

text.clear();

text.insert(0, this.prefix);

}

final String withoutSpaces

= text.toString().replaceAll(" ", "");

text.clear();

text.insert(0, withoutSpaces);

}

// now delete all spaces

this.isInAfterTextChanged = false;

}

}

@Override

public void beforeTextChanged(final CharSequence s,

final int start,

final int count,

final int after) {

// nothing to do here

}

@Override

public void onTextChanged(final CharSequence s,

final int start,

final int before,

final int count) {

// nothing to do here

}

}

它没有太多的代码和逻辑,所以似乎没有第三这种类型的EditText处理需要第三方库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值