按钮单击事件时打开设备联系人列表

时间:2011-02-14 13:16:05

标签: android android-intent onclick contacts

如何在按钮点击事件中打开Android设备联系人列表。

4 个答案:

答案 0 :(得分:9)

试试这段代码..

    yourButton.setOnClickListener(new YouButtonEvent());


    class YouButtonEventimplements OnClickListener{

    @Override
    public void onClick(View v) {

        Intent it= new Intent(Intent.ACTION_PICK,  Contacts.CONTENT_URI);

        startActivityForResult(it, PICK_CONTACT);

    }

}

答案 1 :(得分:4)

声明一些变量。创建方法&处理事件。

private static final int CONTACT_PICKER_RESULT = 1001;
private static final String DEBUG_TAG = "Contact List";
private static final int RESULT_OK = -1;

// a method to open your contact list
private void openContactList() {

    Intent it = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(it, CONTACT_PICKER_RESULT);

}

// handle after selecting a contact from the list
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            // handle contact results
            Log.w(DEBUG_TAG, "Warning: activity result is ok!");
            break;
        }
    } else {
        // gracefully handle failure
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

答案 2 :(得分:3)

您可以将此源代码用作参考:

import android.app.Activity; 
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Test1Activity extends Activity  {

    private static final int PICK_CONTACT_REQUEST = 1;

    private static final int PICK_CONTACT = 0;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button pickContact = (Button) findViewById(R.id.button1);

        pickContact.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) {

                Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
                i.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);   

                startActivity(i);
            }
        });

    }
}

答案 3 :(得分:1)

如果您想从您的设备中选择联系人,请使用此代码。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           openContect();
            dialog.dismiss();
        }

openContact() is:

 private void openContect() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_CONTACT);
    }
}

并在onActivityResult()中使用此内容:

if (requestCode==REQUEST_SELECT_CONTACT && resultCode == RESULT_OK && null != data){
        Uri contactUri = data.getData();
       //do what you want...
    }