我的代码段中有以下按钮。
<Button
android:text="Choose Contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ContactButton"
android:onClick="callContact"
android:layout_marginBottom="31dp"
android:layout_above="@+id/CallButton"
android:layout_centerHorizontal="true" />
我有以下方法(如您所见,在Click上处理):
public void callContact(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
现在我看到了联系人列表。如果我点击它,我问你怎么能从联系人那里得到号码?
android-studio onclick android-contacts contact
答案 0 :(得分:0)
试试这个意图:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
需要调用活动结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver().query(uri, new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
String mobileNumber = c.getString(0);
int type = c.getInt(1);
}
} finally {
if (c != null) {
c.close();
}
}
}
}