简体   繁体   English

使用TextView从通讯簿获取联系人

[英]Use a TextView to get a Contact from Address Book

I'm very new to Android Dev and am having issues trying to get an editText to pull out one contact. 我是Android开发人员的editText在尝试获取editText来拔出一位联系人时遇到了问题。 I've created 3 editText s that will take a contact each and will send a SMS to the selected contact(s). 我创建了3个editText ,每个将接收一个联系人,并将SMS发送到选定的联系人。

http://imgur.com/IbAT1hX http://imgur.com/IbAT1hX

I've tried several things that I've found online, but they all crash and I don't really understand what I'm doing. 我已经尝试了一些在网上找到的东西,但是它们都崩溃了,我不太了解自己在做什么。

I know that I have to create a onClickListener() : 我知道我必须创建一个onClickListener()

    contact.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
      // TODO Auto-generated method stub


      } 
    });

The other question that I have is, since I need to select one contact per EditText, do I have to copy the same code for the three EditText s? 我还有另一个问题,因为我需要为每个EditText选择一个联系人,所以我是否必须为三个EditText复制相同的代码?

After the contact is selected I would like to display just the name of the contact. 选择联系人后,我只想显示联系人的姓名。

Any help would be appreciated! 任何帮助,将不胜感激!

You can do like i did in one of my app (I did this on button click you and use your edittext) Open The contact 您可以像我在一个应用程序中所做的那样操作(我在按钮上执行了此操作,然后单击您并使用您的edittext)打开联系人

button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent= new Intent(Intent.ACTION_PICK,  Contacts.CONTENT_URI);

        startActivityForResult(intent, PICK_CONTACT);

            }
        });

Then after user select you can get the result in OnActivityResult Method and process the contact uri to load details of contact 然后,在用户选择之后,您可以在OnActivityResult方法中获取结果并处理联系人uri以加载联系人的详细信息

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data);

  switch (reqCode) {
    case (PICK_CONTACT) :
      if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor c =  managedQuery(contactData, null, null, null, null);
        if (c.moveToFirst()) {
          String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          // TODO Fetch other Contact details as you want to use
              Set the name of contact in your editText

        }
      }
      break;
  }
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM