मैं टेक्स्टवॉचर के साथ Google के libphonenumber से ASYouTypeFormatter का उपयोग करने की कोशिश कर रहा हूं और यह सुनिश्चित नहीं है कि यह संभव है या नहीं। मैं एक टेक्स्टटेक्स्ट फ़ील्ड से टाइप किए गए टेक्स्ट को प्रारूपित करने में सक्षम हूं और इसे अन्य एडिटटेक्स्ट में आउटपुट करता हूं लेकिन मूल संपादन टेक्स्ट फ़ील्ड को सीधे बदलने में सक्षम नहीं हूं (जो मैं चाहता हूं)। मैं फोन NumberFormattingTextWatcher के बारे में जानता हूं, लेकिन हम चाहते हैं कि उपयोगकर्ता अंततः यह चुनने में सक्षम हो कि वे किस लोकेल में हैं और इसका उपयोग करने से अधिक नियंत्रण है (जो मैंने एकत्र किया है)। वर्ग केमैं एंड्रॉइड ऐप में AsYouTypeFormatter TextWatcher का उपयोग कैसे करूं?
शीर्ष:
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
क्या मैं जानना चाहता है कि कैसे मैं वापस स्वरूपित आउटपुट प्राप्त करना चाहती उत्पत्ति में al EditText के रूप में टाइप किया जा रहा है? जब मैं इसे करने की कोशिश करता हूं, अजीब चीजें डुप्लिकेशंस की तरह होती हैं या यह सिर्फ इसे बिना स्वरूपित दिखाती है। मैंने s.replace() का उपयोग करने का प्रयास किया है, लेकिन मुझे यकीन नहीं है कि मैं इसका उपयोग ठीक से कर रहा हूं। क्या यह संभव है? धन्यवाद?
private String unformattedPhoneNumber;
फिर इस के लिए मेरे कोड को बदलने:
@Override
public void afterTextChanged(Editable s) {
if (!isInAfterTextChanged) {
isInAfterTextChanged = true;
if(s.length() > 0){
Log.v("AsYouTypeFormatter - source", s.toString());
unformattedPhoneNumber = s.toString().replaceAll("[^\\d.]", "");
for(int i = 0; i < unformattedPhoneNumber.length(); i++){
formattedPhoneNumber = aytf.inputDigit(unformattedPhoneNumber.charAt(i));
Log.v("AsYouTypeFormatter - formatted", formattedPhoneNumber);
}
Log.v("AsYouTypeFormatter - source after loop", s.toString());
phoneNumberText.setText(formattedPhoneNumber);
aytf.clear();
}
formattedPhoneNumber = null;
isInAfterTextChanged = false;
}
}
ऐसा लगता है कि aytf में सक्षम नहीं था
सिर्फ PhoneNumberFormattingTextWatcher का उपयोग क्यों नहीं (स्ट्रिंग देश कोड) ??? http://stackoverflow.com/a/29109236/1103584 – DiscDev
इसके लिए minSdkVersion 21 –