在 API 16 上使用 PhoneNumberUtils.formatNumber() [英] Using PhoneNumberUtils.formatNumber() on API 16
问题描述
我正在尝试将数字格式化为默认的国家/地区代码,我知道如何设置,但是当我这样做时,会出现一个错误,指出这仅适用于 API 21.我的目标是 API 16.如果我使用旧方法,我收到一条错误消息,说该方法已被弃用?如何在 API 16 上使用该方法?
I'm trying to format numbers to a default country code, and I know how, but when I do it, an error appears saying this is only for API 21. I am targeting API 16. If I use the old method, I get an error saying the method is deprecated? How can I use that method on API 16?
谢谢!
文档:http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html#FORMAT_NANP
推荐答案
@qbix 提到的带有弃用方法的以下示例.
Following example with deprecated method as mentioned by @qbix.
一个好的做法是检查sdk的级别以使用正确的方法:
A good practice is to check the level of the sdk to use the correct method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
yourTextView.setText(PhoneNumberUtils.formatNumber(yourStringPhone, Locale.getDefault().getCountry()));
} else {
yourTextView.setText(PhoneNumberUtils.formatNumber(yourStringPhone)); //Deprecated method
}
这篇关于在 API 16 上使用 PhoneNumberUtils.formatNumber()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!