从输入框中,我抛出一个对话框来获取联系人。我的代码如下所示
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toContacts = (EditText)findViewById(R.id.To);
toContacts.setOnClickListener(new EditText.OnClickListener(){public void onClick(View v){pickContacts();}});
}
public void pickContacts(){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
String name = "";
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
//@SuppressWarnings("deprecation")
Cursor c = null;
try{
//c = managedQuery(contactData, null, null, null, null);
c = getContentResolver().query(contactData, null, Contacts.DISPLAY_NAME, null, null);
if (c.moveToFirst()) {
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
} catch(Exception e){
Log.e(DEBUG_TAG, "failed to get contacts data", e);
} finally {
if(c != null){
c.close();
}
EditText toSendContact = (EditText) findViewById(R.id.To);
toSendContact.setText(name);
}
}
break;
}
}
但它不会在输入框中返回选定的结果。