- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用带有 TextWatcher 的 EditText 创建 mask 。这个面具需要格式化一个电话号码,但问题是我有两种情况使用这个面具。情况 1 掩码需要有 13 位 (99)9999-9999
,情况 2 需要有 14 位 (99)99999-9999
。我希望在我键入数字时进行格式化。
我该怎么做?
我正在尝试这个。
//Activity
private EditText etPhone;
etPhone = (EditText)findViewById(R.id.etPhone());
etPhone.addTextChangedListener(new TelefoneMask(etPhone));
//TextWatcher
public class TelefoneMask implements TextWatcher {
private String current;
private EditText phone;
private StringBuilder mask = new StringBuilder();
public TelefoneMask(EditText phone) {
this.phone = phone;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public synchronized void afterTextChanged(Editable s) {
// mask1 -> (99)9999-9999 13 digits
// mask2 -> (99)99999-9999 14 digits
Integer size = s.length();
if(size == 10){
mask.append(s.toString());
mask.insert(0, "(");
mask.insert(3, ")");
mask.insert(8, "-");
}else if(size > 10){
mask.setLength(0);
mask.append(s.toString());
mask.insert(0, "(");
mask.insert(3, ")");
mask.insert(9, "-");
}
current = mask.toString();
phone.setText(current);
phone.setSelection(mask.length());
}
}
最佳答案
解决了问题
我做了
public abstract class EditTextTelefoneMask {
private static final String mask8 = "####-####";
private static final String mask9 = "#####-####";
private static final String mask10 = "(##) ####-####";
private static final String mask11 = "(##) #####-####";
public static String unmask(String s) {
return s.replaceAll("[^0-9]*", "");
}
public static TextWatcher insert(final EditText editText) {
return new TextWatcher() {
boolean isUpdating;
String old = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = EditTextTelefoneMask.unmask(s.toString());
String mask;
String defaultMask = getDefaultMask(str);
switch (str.length()) {
case 11:
mask = mask11;
break;
case 10:
mask = mask10;
break;
case 9:
mask = mask9;
break;
default:
mask = defaultMask;
break;
}
String mascara = "";
if (isUpdating) {
old = str;
isUpdating = false;
return;
}
int i = 0;
for (char m : mask.toCharArray()) {
if ((m != '#' && str.length() > old.length()) || (m != '#' && str.length() < old.length() && str.length() != i)) {
mascara += m;
continue;
}
try {
mascara += str.charAt(i);
} catch (Exception e) {
break;
}
i++;
}
isUpdating = true;
editText.setText(mascara);
editText.setSelection(mascara.length());
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
public void afterTextChanged(Editable s) {}
};
}
private static String getDefaultMask(String str) {
String defaultMask = mask8;
if (str.length() > 11){
defaultMask = mask11;
}
return defaultMask;
}
}
Activity
etPhone.addTextChangedListener(EditTextTelefoneMask.insert(etPhone));
关于android - 使用 TextWatcher 的动态蒙版?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659764/
什么情况下应该使用afterTextChanged而不是onTextChanged,反之亦然? 最佳答案 这些事件按以下顺序调用: beforeTextChanged(CharSequence s,
我正在做一个聊天应用程序,并了解如何制作 textWatcher,它将在用户输入的情况下推送 firebase 数据结构.我想推送一个数据结构,在该数据结构上您将看到用户是否正在输入。当用户键入时,用
我有一个自定义列表适配器,其中一个列表项具有三个编辑文本。我有一个日期选择器供其中之一使用。设置日期后,适配器的相应型号会正确保存日期。但是对于其他编辑文本,我只想将输入的文本存储在列表中。我使用 t
我正在尝试让 TextWatcher 与 AutoCompleteTextView 一起工作,但遇到了一些问题。我面临的问题是,当我开始输入文本时,如果数据输入的长度只有一个字符,我在 AutoCom
自从我开始使用 TextWatcher 以来,我的应用程序不断崩溃...正如你在下面看到的,我为 3 个 EditText 字段创建了一个 TextWatcher...我做了一个按钮来监听 3 个 E
我正在尝试配置一个简单的表单。我有 3 个编辑文本,在用户结束输入后,我想显示选中的图标(如果有效)(并转到下一个字段),如果无效则显示错误,用户可以更正。 我的主要问题是我只能输入 1 个字符,然后
我的 Android 应用程序上有一个 TextWatcher。问题是我希望它监听每个文本更改,并 append 一个相同的 TextView 字符串。 例如,用户在 EditText 字段上输入 A
我已将 TextWatcher 添加到 EditText 并通过 onTextChanged(CharSequence s, int start, int before, int count) 方法监
在我按下退格键之后,我的 textwatcher 开始工作,然后那个建议出现了。我有一个 textwatcher,可以从网络生成书籍列表。我也等着看它是否是一个网络问题。但我发现不是因为那个,当我按退
我正在尝试过滤 gridview,为此我在编辑框中输入数据。它工作得很好,除了当我在编辑框中插入一个空格字符时,它不知何故无法识别它并且结果集为空。请帮忙 这是我的编辑框监听器: search.add
我在 EditText 字段上有提示文本和普通文本,我指定了不同的 Font-Size、Font-Color、Font-Name 两者。我已经把它放在我的 ActivityClass 中,它工作正常。
我有两个编辑文本,我想在编辑另一个文本的同时更改一个文本,反之亦然。我使用了 TextWatcher,它或多或少是这样的: //MUDAR AO DIGITAR edt1.addTe
我是 Android 开发的新手,目前正在开发一款应用程序,该应用程序允许人们在框中键入文本,文本会显示在屏幕上的位图上。 我在监听变化的 Activity 中有一个文本框。每次键入文本时,它都会调用
每次将“q”写入我附加 TextWatcher 的编辑文本的最后一个字符时,“q”就会被“a”替换。我使用: public void afterTextChanged(Editable s) {
我的代码看起来很简单,但我不知道问题出在哪里。如果我编写这样的代码: tvResult.setText(sum+ "RUB"); 它显示了正确的数字。但如果我尝试添加这样的 IF 语句: if(sum
在我的应用程序中,我将 TextWatcher 放在 EditText 上。当我更改 EditText 的文本时,TextWatcher 的事件被调用两次。 我正在使用模拟器来运行该应用程序。 最佳答
我有一个 Android 编程问题。使用下面的代码我想验证一个字符串匹配。它验证正常,但 LogCat 显示 TextWatcher 方法在每次击键时触发两次,我不明白为什么。我希望每次击键只触发一次
TextWatcher 有一个烦人的问题。我一直在网上搜索,但找不到任何东西。如果有人可以帮助我,将不胜感激。 出于某种原因,在一个文本更改时调用 TextWatcher 事件是不稳定的。有时它们会被
在我的应用程序中,我想跟踪 EditText 中的输入。每次用户修改 EditText 中的字符时,我想跟踪: 如果添加/删除了字符 添加/删除字符的索引以及 添加/删除了什么字符 我使用TextWa
我想使用 TextWatcher 为多个 editText 编写一个 validator 。 我已经有方法来验证用户写入的数据是否正确。还添加了验证来检查 editText 是否为空。但这里它没有像我
我是一名优秀的程序员,十分优秀!