尝试使用联系人选择器将联系人插入编辑文本
Posted
技术标签:
【中文标题】尝试使用联系人选择器将联系人插入编辑文本【英文标题】:Trying to insert contact into edittext using contact picker 【发布时间】:2012-07-31 16:04:20 【问题描述】:我正在尝试允许用户使用 android 中的联系人选择器插入他们的号码。我目前正在使用Getting Number from Contacts Picker 中的帖子 2 中的示例,联系人选择器会出现,但是当我选择联系人时,联系人号码不会影响我的编辑文本。 没有 logcat 错误或任何错误。
我的代码:
public void doLaunchContactPicker(View view)
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode == RESULT_OK)
switch (requestCode)
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phoneNumber = "";
List<String> allNumbers = new ArrayList<String>();
int phoneIdx = 0;
try
Uri result = data.getData();
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] id , null);
phoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst())
while (cursor.isAfterLast() == false)
phoneNumber = cursor.getString(phoneIdx);
allNumbers.add(phoneNumber);
cursor.moveToNext();
else
//no results actions
catch (Exception e)
//error actions
finally
if (cursor != null)
cursor.close();
final EditText phoneInput = (EditText) findViewById(R.id.mobileno);
final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(SIMMessageSenderActivity.this);
builder.setTitle("Choose a number");
builder.setItems(items, new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int item)
String selectedNumber = items[item].toString();
selectedNumber = selectedNumber.replace("-", "");
phoneInput.setText(selectedNumber);
);
AlertDialog alert = builder.create();
if(allNumbers.size() > 1)
alert.show();
else
String selectedNumber = phoneNumber.toString();
selectedNumber = selectedNumber.replace("-", "");
phoneInput.setText(selectedNumber);
if (phoneNumber.length() == 0)
//no numbers found actions
break;
else
//activity result error actions
【问题讨论】:
我不知道你的问题是什么。请更好地描述它并更新您的帖子。 这只是意味着当我单击联系人时,联系人号码不会插入到我的编辑文本中?什么时候应该有我在编辑文本中选择的联系人号码 为了清晰起见更新您的帖子。不要把它放在 cmets 中。 【参考方案1】:下面的代码是我正在使用的代码,它对我来说工作得很好。试试这个。
if((requestCode == PICK_CONTACT) && (resultCode == RESULT_OK))
if (data != null)
Uri contactData = data.getData();
try
String id = contactData.getLastPathSegment();
String[] columns = Phone.DATA,Phone.DISPLAY_NAME;
Cursor phoneCur = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
columns ,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] id ,
null);
final ArrayList<String> phonesList = new ArrayList<String>();
String Name = null ;
if(phoneCur.moveToFirst())
do
Name = phoneCur.getString(phoneCur.getColumnIndex(Phone.DISPLAY_NAME));
String phone = phoneCur
.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
phonesList.add(phone);
while (phoneCur.moveToNext());
phoneCur.close();
if (phonesList.size() == 0)
Toast.makeText(
this,"This contact does not contacin any number",
Toast.LENGTH_LONG).show();
else if (phonesList.size() == 1)
toET.setText(phonesList.get(0));
else
final String[] phonesArr = new String[phonesList
.size()];
for (int i = 0; i < phonesList.size(); i++)
phonesArr[i] = phonesList.get(i);
AlertDialog.Builder dialog = new AlertDialog.Builder(
MessageManagerActivity.this);
dialog.setTitle("Name : "+Name);
((Builder) dialog).setItems(phonesArr,
new DialogInterface.OnClickListener()
public void onClick(
DialogInterface dialog,
int which)
String selectedEmail = phonesArr[which];
toET.setText(selectedEmail);
).create();
dialog.show();
catch (Exception e)
Log.e("FILES", "Failed to get phone data", e);
将你的edittext创建为一个类变量..这样你也可以应用一个空检查,我非常确定你正在为此再次创建它..
【讨论】:
以上是关于尝试使用联系人选择器将联系人插入编辑文本的主要内容,如果未能解决你的问题,请参考以下文章
Swift 2.0 Xcode 7.1 图像选择器前置摄像头图像在选择时旋转