问题描述
我正在尝试将数字格式化为默认的国家/地区代码,并且我知道如何做,但是当我这样做时,会出现一条错误消息,说这仅适用于API21.我的目标是API16.如果我使用的是旧方法,我收到一条错误消息,说该方法已过时?如何在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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!