1# 使用picker管理联系人 2 3<!--Kit: Contacts Kit--> 4<!--Subsystem: Applications--> 5<!--Owner: @librahCode--> 6<!--Designer: @yanghaoqian--> 7<!--Tester: @shangzhijie--> 8<!--Adviser: @zhang_yixin13--> 9## 接口说明 10 11| 接口名 | 描述 | 12| --------------------- | ------------------------------------------ | 13| addContactViaUI(context: Context, contact: Contact): Promise<number> | 调用新建联系人接口,打开新建联系人UI界面,新建完成。使用Promise异步回调。 | 14| saveToExistingContactViaUI(context: Context, contact: Contact): Promise<number> | 调用保存至已有联系人接口,选择联系人UI界面并完成编辑。使用Promise异步回调。 | 15 16 17## 使用picker新建联系人 18 19调用新建联系人接口,打开新建联系人UI界面,用户可在UI界面中填写并新建联系人。 20 21```js 22import { common } from '@kit.AbilityKit'; 23import { contact } from '@kit.ContactsKit'; 24import { BusinessError } from '@kit.BasicServicesKit'; 25 26 27@Entry 28@Component 29struct Index { 30 @State message: string = 'Hello World'; 31 32 build() { 33 Column() { 34 Text(this.message) 35 .fontSize(50) 36 .fontWeight(FontWeight.Bold) 37 .onClick(() => { 38 let contactInfo: contact.Contact = { 39 name: { 40 fullName: 'xxx' 41 }, 42 phoneNumbers: [{ 43 phoneNumber: '138xxxxxx' 44 }] 45 } 46 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 47 let promise = contact.addContactViaUI(context, contactInfo); 48 promise.then((data) => { 49 console.info(`Succeeded in add Contact via UI.data->${JSON.stringify(data)}`); 50 }).catch((err: BusinessError) => { 51 console.error(`Failed to add Contact via UI. Code: ${err.code}, message: ${err.message}`); 52 }); 53 }) 54 } 55 } 56} 57``` 58 59## 使用picker更新联系人信息 60 61可以通过拉起picker,将选中的联系人信息更新到现有联系人中。 62 63```js 64import { common } from '@kit.AbilityKit'; 65import { contact } from '@kit.ContactsKit'; 66import { BusinessError } from '@kit.BasicServicesKit'; 67 68 69@Entry 70@Component 71struct Index { 72 @State message: string = 'Hello World'; 73 74 build() { 75 Column() { 76 Text(this.message) 77 .fontSize(50) 78 .fontWeight(FontWeight.Bold) 79 .onClick(() => { 80 let contactInfo: contact.Contact = { 81 id: 1, 82 name: { 83 fullName: 'xxx' 84 }, 85 phoneNumbers: [{ 86 phoneNumber: '138xxxxxx' 87 }] 88 } 89 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 90 let promise = contact.saveToExistingContactViaUI(context, contactInfo); 91 promise.then((data) => { 92 console.info(`Succeeded in save to existing Contact via UI.data->${JSON.stringify(data)}`); 93 }).catch((err: BusinessError) => { 94 console.error(`Failed to save to existing Contact via UI. Code: ${err.code}, message: ${err.message}`); 95 }); 96 }) 97 } 98 } 99} 100```