HarmonyOS:电话号码格式化

81 阅读2分钟

一、使用场景

不同国家和地区的电话号码在号码位数、组合方式、呈现方式等都存在差异。同时,在不同环境和条件下,电话号码可能存在不同的拨打方式和号码格式。例如,在中国境内跨地区打电话,通常需要先输入“0”,再拨打区号和八位电话号码,而在香港或澳门拨打电话时,需要不同的拨号方式。
通过号码格式化,可以根据需要给用户展示特定格式的电话号码。

二、开发步骤

电话号码格式化通过PhoneNumberFormatformat接口实现,具体开发步骤如下

2.1 导入模块。

import { i18n } from '@kit.LocalizationKit';

2.2 创建PhoneNumberFormat对象。

构造函数支通过PhoneNumberFormatOptions设置不同的电话号码格式,具体请参考表1。

let phoneNumberFormat: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat(country: string, options?: PhoneNumberFormatOptions);

2.3 电话号码格式化

let formattedPhoneNumber: string = phoneNumberFormat.format(phoneNumber: string);

2.4 判断电话号码正确性和号码归属地

let isValidNumber: boolean = phoneNumberFormat.isValidNumber(phoneNumber: string); // 判断电话号码正确性
let locationName: string = phoneNumberFormat.getLocationName(number: string, locale: string); // 获取号码归属地

2.5 电话号码格式化选项

以电话号码158****2312,所属国家代码CN为例,说明PhoneNumberFormatOptions不同取值和显示效果。

表1 电话号码格式化的类型(type)

取值显示效果
E164+86 158****2312
INTERNATIONAL+86 158 **** 2312
NATIONAL158 **** 2312
RFC3966tel:+86-158-****-2312
TYPING158 ***

2.6 开发实例

效果图
在这里插入图片描述

TestphoneNumberFormat.ets代码

// 导入模块
import { i18n } from '@kit.LocalizationKit';

// 格式化电话号码
let phoneNumberFormat1 = new i18n.PhoneNumberFormat('CN');
let formattedPhoneNumber1 = phoneNumberFormat1.format('158****2312'); // formattedPhoneNumber1: 158 **** 2312
console.log(` 格式化电话号码 formattedPhoneNumber1:${formattedPhoneNumber1}`)

// RFC3966类型的电话号码
let phoneNumberFormat2 = new i18n.PhoneNumberFormat('CN', { type: 'RFC3966' });
let formattedPhoneNumber2 = phoneNumberFormat2.format('158****2312'); // formattedPhoneNumber2: tel:+86-158-****-2312
console.log(` RFC3966类型的电话号码 formattedPhoneNumber2:${formattedPhoneNumber2}`)

// 判断电话号码是否有效
let phoneNumberFormat3 = new i18n.PhoneNumberFormat('CN');
let isValid = phoneNumberFormat3.isValidNumber('158****2312'); // isValid: true
console.log(` 判断电话号码是否有效 isValid:${isValid}`)

// 以某种语言显示号码归属地
let phoneNumberFormat4 = new i18n.PhoneNumberFormat("CN");
let locationName4 = phoneNumberFormat4.getLocationName('158****2312', 'en-GB') // locationName4: XiAn, Shanxi
console.log(` 以某种语言显示号码归属地 locationName4:${locationName4}`)

// 拨号中的电话号码格式化
let phoneNumberFmt = new i18n.PhoneNumberFormat('CN', { type: 'TYPING' });
let phoneNumber: string = "0755453";
let formatResult: string = "";
for (let i = 0; i < phoneNumber.length; i++) {
  formatResult += phoneNumber.charAt(i);
  formatResult = phoneNumberFmt.format(formatResult);
}
console.log(` 拨号中的电话号码格式化 formatResult:${formatResult}`); // formatResult: 0755 453

@Entry
@Component
struct TestphoneNumberFormat {
  @State message: string = '电话号码格式化';

  build() {
    Column({ space: 16 }) {
      Text(this.message)
        .id('TestphoneNumberFormatHelloWorld')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
      Text(`格式化电话号码:${formattedPhoneNumber1}`).fontSize(20).fontWeight(FontWeight.Medium)
      Text(`RFC3966类型的电话号码:${formattedPhoneNumber2}`).fontSize(20).fontWeight(FontWeight.Medium)
      Text(`判断电话号码是否有效:${isValid}`).fontSize(20).fontWeight(FontWeight.Medium)
      Text(`以某种语言显示号码归属地:${locationName4}`).fontSize(20).fontWeight(FontWeight.Medium)
      Text(`拨号中的电话号码格式化:${formatResult}`).fontSize(20).fontWeight(FontWeight.Medium)
    }
    .height('100%')
    .width('100%')
  }
}