如何在Android App中使用AsYouTypeFormatter TextWatcher?
我正在尝试将Google的libphonenumber中的AsYouTypeFormatter与TextWatcher一起使用,但不确定这是否可能。我已经能够格式化从EditText字段键入的文本并将其输出到另一个EditText中,但无法直接更改原始的EditText字段(这是我想要的)。我知道phoneNumberFormattingTextWatcher,但我们希望用户最终能够选择他们所在的区域设置,并且比使用它允许的有更多的控制权(从我收集到的信息中)。
顶级:
private View phoneNumberView;
private EditText phoneNumberText;
private String formattedPhoneNumber;
private boolean isInAfterTextChanged;
private AsYouTypeFormatter aytf;
首先,我初始化类顶部的AsYouTypeFormatter:
aytf = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(Locale.getDefault().getCountry());
创建选项卡等后,我创建了一个EditText框,格式化其中的现有电话号码(有效),并附加侦听器(这是当前类):
phoneNumberView = inflater.inflate(R.layout.phone_number_setting, null);
phoneNumberText = (EditText) phoneNumberView.findViewById(R.id.dirty_phone_number);
phoneNumberText.setText(dirtyPhoneNumber);
for (int i = 0; i < dirtyPhoneNumber.length(); i++){
phoneNumberText.setText(aytf.inputDigit(dirtyPhoneNumber.charAt(i)));
}
aytf.clear();
phoneNumberText.setText(dirtyPhoneNumber);
phoneNumberText.addTextChangedListener(this);
然后这就是我目前在TextWatcher函数中所拥有的,我不确定我的代码应该在以下哪个函数中:
@Override
public void afterTextChanged(Editable s) {
if (!isInAfterTextChanged) {
isInAfterTextChanged = true;
if(s.length() > 0){
Log.v("AsYouTypeFormatter - source", s.toString());
for(int i = 0; i < s.length(); i++){
formattedPhoneNumber = aytf.inputDigit(s.charAt(i));
Log.v("AsYouTypeFormatter - formatted", formattedPhoneNumber);
}
Log.v("AsYouTypeFormatter - source after loop", s.toString());
//The formatted output shows properly in this EditText but not when I try to put it back into the original one (phoneNumberText)
EditText testPhoneNumberText = (EditText) phoneNumberView.findViewById(R.id.testPhoneNumberText);
testPhoneNumberText.setText(formattedPhoneNumber);
aytf.clear();
}
formattedPhoneNumber = null;
isInAfterTextChanged = false;
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
下面是记录的输出的示例,因此我知道格式设置正在起作用:
01-03 10:55:02.838: V/AsYouTypeFormatter - source(27114): 15552451234
01-03 10:55:02.838: V/AsYouTypeFormatter - formatted(27114): 1
01-03 10:55:02.838: V/AsYouTypeFormatter - formatted(27114): 15
01-03 10:55:02.838: V/AsYouTypeFormatter - formatted(27114): 1 55
01-03 10:55:02.838: V/AsYouTypeFormatter - formatted(27114): 1 555
01-03 10:55:02.838: V/AsYouTypeFormatter - formatted(27114): 1 555-2
01-03 10:55:02.838: V/AsYouTypeFormatter - formatted(27114): 1 555-24
01-03 10:55:02.850: V/AsYouTypeFormatter - formatted(27114): 1 555-245
01-03 10:55:02.850: V/AsYouTypeFormatter - formatted(27114): 1 555-245-1
01-03 10:55:02.850: V/AsYouTypeFormatter - formatted(27114): 1 555-245-12
01-03 10:55:02.850: V/AsYouTypeFormatter - formatted(27114): 1 555-245-123
01-03 10:55:02.850: V/AsYouTypeFormatter - formatted(27114): 1 555-245-1234
01-03 10:55:02.850: V/AsYouTypeFormatter - source after loop(27114): 15552451234
这是输出的图像(顶部 EditText 是 phoneNumberText,底部是 testPhoneNumberText):http://imgur.com/GXwRu.png
我想知道的是,如何在键入格式化输出时将其恢复到原始EditText中?当我尝试这样做时,会发生一些奇怪的事情,比如重复,或者只是显示它没有格式化。我尝试过使用s.replace(),但我不确定我是否正确地使用它。这可能吗?谢谢?