2017-02-21 79 views
0

我想設置PhoneNumberFormattingTextWatcher的語言環境。如何在kitkat中設置PhoneNumberFormattingTextWatcher的語言環境?

但是在kitkat中,不能使用PhoneNumberFormattingTextWatcher(String countryCode)構造函數。

所以我嘗試使用反射。喜歡這個。

phoneNumberFormattingTextWatcher = new phoneNumberFormattingTextWatcher(); 

Field mFormatter = PhoneNumberFormattingTextWatcher.class.getDeclaredField("mFormatter"); 
mFormatter.setAccessible(true); 

mFormatter.set(phoneNumberFormattingTextWatcher, 
PhoneNumberUtil.getInstance().getAsYouTypeFormatter(COUNTRY_ISO)); 

但mFormatter.set()方法拋出IllegalArgumentException。

我該如何解決?


編譯 'com.googlecode.libphonenumber:libphonenumber:7.2.2'

的build.gradle(應用程序)

+0

看起來像使用'String'的隱藏構造函數更容易:https://android.googlesource.com/platform/frameworks/base.git/+/kitkat-release/telephony/java/ android/telephony/PhoneNumberFormattingTextWatcher.java#71不知道爲什麼它被隱藏了。 –

+0

訪問隱藏的構造函數..這是一個好主意。我會盡力! –

回答

0

PhoneNumberFormattingTextWatcher的mFormatter是 com.android.i18n.phonenumbers.AsYouTypeFormatter類,但我注入了com.google.i18n.phonenumbers.AsYouTypeFormatter類的實例。 T T

1

mPhoneText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

這不適用於21或更高api。所以你應該像這樣包裝在api lvl條件下。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      mPhoneText.addTextChangedListener(new PhoneNumberFormattingTextWatcher("US")); 
     }else { 
      mPhoneText.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); 
     } 

不要忘記設置inputType="phone"

你這是怎麼只能爲更高API設置的地點。

+0

我想在其他情況下使用PhoneNumberFormattingTextWatcher(「US」)(在棒棒糖下)。 我已經使用過(Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP)這種情況。 –

+1

好..它不適用於較低版本..但你可以使用不同的庫。 –