1、添加AddressBook1+MultiView1+speedbuttonX3+labelX#+editboxX3+buttonX2+listbox组件。
2、对listbox右键additem选择tsearchbox类型。
3、设置MultiView1的属性。
4、设置edit1、edit2、edit3的属性。
KillFocusByReturn:=true;
ReturnKeyType:=Go;
5、编写罗列通讯录以及添加通讯录代码。
procedure TForm1.FillContactList;
var
I: Integer;
Contacts: TAddressBookContacts;
begin
Contacts := TAddressBookContacts.Create;
try
AddressBook1.AllContacts(AddressBook1.DefaultSource, Contacts);
ListBox1.BeginUpdate;
try
ListBox1.Clear;
for I := 0 to Contacts.Count - 1 do
AddListBoxItem(Contacts.Items[I]);
finally
ListBox1.EndUpdate;
end;
finally
Contacts.Free;
end;
end;
procedure TForm1.AddListBoxItem(Contact: TAddressBookContact);
var
ListBoxItem: TListBoxItem;
begin
try
ListBoxItem := TListBoxItem.Create(nil);
ListBoxItem.Text := Contact.DisplayName;
ListBoxItem.Tag := Contact.ID;
ListBox1.AddObject(ListBoxItem);
finally
ListBoxItem.Free;
end;
end;
6、添加删除某条记录以及刷新代码。
procedure TForm1.SpeedButton4Click(Sender: TObject);
var
Contactlndex, ContactID: Integer;
Contact: TAddressBookContact;
begin
Contactlndex := ListBox1.Itemindex;
if (Contactlndex > -1) then
begin
ContactID := ListBox1.Listitems[Contactlndex].Tag;
Contact := AddressBook1.ContactByID(ContactID);
if Contact <> nil then
try
AddressBook1.RemoveContact(Contact);
ListBox1.BeginUpdate;
ListBox1.Items.Delete(Contactlndex);
finally
ListBox1.EndUpdate;
Contact.Free;
end;
end;
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
FillContactList;
end;
添加通讯录记录代码。
procedure TForm1.Button1Click(Sender: TObject);
var
Contact: TAddressBookContact;
eMails: TContactEmails;
Addresses: TContactAddresses;
begin
Contact := AddressBook1.CreateContact(AddressBook1.Defaultsource);
try
Contact.FirstName := edtFirstName.Text;
Contact.LastName := edtLastName.Text;
// Add the work mail
eMails := TContactEmails.Create;
try
eMails.AddEmail(TContactEmail.TLabelKind.Work, edtWorkEmail.Text);
Contact.eMails := eMails;
finally
eMails.Free;
end;
AddressBook1.SaveContact(Contact);
try
ListBox1.BeginUpdate;
AddListBoxItem(Contact);
finally
ListBox1.EndUpdate;
end;
finally
Contact.Free;
end;
MultiView1.HideMaster;
end;
取消按钮代码。
procedure TForm1.Button2Click(Sender: TObject);
begin
edtFirstName.Text := '';
edtLastName.Text := '';
edtWorkEmail.Text := '';
MultiView1.HideMaster;
end;
要求获得权限代码。
uses
FMX.DialogService;
procedure TForm1.FormShow(Sender: TObject);
begin
// Display this information box while loading the contacts
if AddressBook1.Supported then
begin
TDialogService.ShowMessage(' Loading contacts ...');
AddressBook1.RequestPermission;
end
else
showmessage('This platform does not support the Address Book service1');
end;
如果有权限执行读入代码。
procedure TForm1.AddressBook1PermissionRequest(ASender: TObject; const AMessage: string; const AAccessGranted: Boolean);
begin
if AAccessGranted then
begin
FillContactlist;
end
else
ShowMessage('You cannot access Address Book. Reason: ' + AMessage);
end;
设置app的使用权限。