2013-01-03 14 views
8

मैं टेक्स्टवॉचर के साथ 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 में सक्षम नहीं था

+1

सिर्फ PhoneNumberFormattingTextWatcher का उपयोग क्यों नहीं (स्ट्रिंग देश कोड) ??? http://stackoverflow.com/a/29109236/1103584 – DiscDev

+0

इसके लिए minSdkVersion 21 –

उत्तर

3

मैं शीर्ष पर एक नया स्ट्रिंग घोषित करने समाप्त हो गया पहले से ही आंशिक रूप से स्वरूपित फोन नंबरों को प्रारूपित करने के लिए, इसलिए मुझे एटीएफ को पुनः सबमिट करने से पहले सभी गैर अंकों को बाहर करना पड़ा? अब एकमात्र समस्या यह है कि एडिटटेक्स्ट फ़ील्ड में कर्सर अभी अंत की बजाय शुरुआत में है, लेकिन इसे ठीक करने में कोई समस्या नहीं होनी चाहिए। वाह।

संपादित कोड:

@Override 
public void afterTextChanged(Editable s) { 
    if (!isInAfterTextChanged) { 
     isInAfterTextChanged = true; 
     phoneNumberText.setText(pnu.updateNationalNumber(s.toString())); 
     phoneNumberText.setSelection(this.phoneNumberText.getText().length()); 
     isInAfterTextChanged = false; 
    } 
} 

/** 
* Updates the national number based on the param s 
* Takes all formatting out of param s and then reformats the number 
* using the AsYouTypeFormatter for libphonenumber and based upon 
* the region code 
* 
* @param s The formatted value to be used to update the national number 
* @return String The new formatted national number 
*/ 
public String updateNationalNumber(String s){ 
    //Instantiate the as you type formatter with the current region (US or UK) 
    aytf = phoneUtil.getAsYouTypeFormatter(this.currentRegionCode.getCountryCode()); 
    String fNationalNumber = null; 

    //Format the string 
    if(s.length() > 0){ 
     String digitString = null; 
     //If it's in the US remove all leading 1s (international code) 
     if(this.currentRegionCode == RegionCode.US){ 
      digitString = new String(s.replaceAll("(^[1?])|([^\\d.])", "")); 
     } 
     //If it's in the UK remove all leading 44s (international code) 
     else if (this.currentRegionCode == RegionCode.GB){ 
      digitString = new String(s.replaceAll("(^[4?]{2})|([^\\d.])", "")); 
     } 
     if(digitString != null){ 
      //RE input all of the digits into the formatter 
      for(int i = 0; i < digitString.length(); i++){ 
       fNationalNumber = aytf.inputDigit(digitString.charAt(i)); 
      } 
     } 

     //Clear the formatter for the next round of input 
     aytf.clear(); 

     //Try to update the phone number with the formatted number 
     try { 
      phoneUtil.parse(fNationalNumber, this.currentRegionCode.getCountryCode(), this.uPhoneNumber); 
     //Rejects if the number isn't in an acceptable format for the region code given etc. 
     } catch (NumberParseException e) { 
      System.err.println("NumberParseException was thrown: " + e.toString()); 
     } 
    } 
    //Return the formatted phone number 
    return fNationalNumber; 
} 
+0

की आवश्यकता है, मैं विशेष चर जैसे +, (,) आदि जोड़ सकता हूं ... क्या करना है? –

+0

आपको इस फ़ॉर्मेटर के साथ +, (,) जोड़ने की आवश्यकता नहीं है, इसे आपके लिए जोड़ना चाहिए। क्या आप यह स्पष्ट कर सकते हैं कि आप क्या करने की कोशिश कर रहे हैं? – sugarwaffle

+0

मान लीजिए कि उपयोगकर्ता इच्छित वर्णों के साथ संख्याओं को दर्ज करने का प्रयास करता है, मुझे लगता है कि इसे स्वीकार करना चाहिए :) –

4

वहाँ बाहर दूसरों जो सिर्फ उपयोगकर्ता प्रकार के रूप में एक EditText में एक उपयोगकर्ता के द्वारा दर्ज फ़ोन नंबर स्वरूपित करना चाहते हैं के लिए, यह बहुत, बहुत आसान PhoneNumberFormattingTextWatcher उपयोग करने के लिए (एंड्रॉयड करने के लिए बनाया है) इनमें से किसी भी वर्बोज़ उत्तर का प्रयास करने की तुलना में - और यह कोड की एक पंक्ति है!

//Add a special listener for this instance that will format phone numbers on the fly. 
this.editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); 

तुम भी क्षेत्र, उपयोगकर्ता का चयन किया है जो मुझे लगता है कि वास्तव में ओ पी के सवाल का जवाब होगा पारित कर सकते हैं, लेकिन यह जब तक उपलब्ध नहीं था एपीआई 21:

//This version takes a country code! 
this.editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher("US")); 
+0

इसके लिए आपको minSdkVersion 21 घोषित करने की आवश्यकता है, जो शायद –

+2

@LouisMorda केवल 21 वर्ष के minSdkVersion की आवश्यकता वाले क्षेत्र को पार कर रहा है - आप इस क्षेत्र को अन्य तरीकों से ट्रैक कर सकते हैं (जैसे कि मैं अपने प्रोजेक्ट में करता हूं) कम minSdkVersion, मैं बस यह इंगित कर रहा था कि यह उपलब्ध है। – DiscDev

+1

कई अन्य एसओएफ पदों और मेरे व्यक्तिगत अनुभव के अनुसार, यह केवल सीमित संख्या में उपकरणों पर काम करता है। क्यों, मुझे नहीं पता। – Nick

संबंधित मुद्दे