Android Contact Picker [多个hp编号以及如何在EditText中显示]

时间:2016-12-18 08:45:36

标签: android android-contacts

我想创建3个TextView字段,每个TextView字段显示所选的联系人号码。 我仅使用1个TextView成功,但我不知道处理多个联系人。

contactNumber = (TextView) findViewById(R.id.contactnumber);
Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, 1);
        }});
下面的

显示了我如何显示1个联系人的hp号码,但如何显示多个电话号码?例如,TextView1显示123,TextView2显示456等。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RQS_PICK_CONTACT){
        if(resultCode == RESULT_OK){
            Uri contactData = data.getData();
            Cursor cursor =  managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();

            String number =       cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            //contactName.setText(name);
            contactNumber.setText(number);
            //contactEmail.setText(email);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您只需运行三次代码,并在每个代码上使用不同的TextView ID,Button ID和RequestCode

您可以使用辅助方法,如下所示:

private void setupViews(long textViewId, long buttonId, final int requestCode) {
   TextView tv = (TextView) findViewById(textViewId);
   Button button = (Button) findViewById(buttonId);
   button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, requestCode);
        }
   });
}

为每个TextView - Button对调用3次:

setupViews(R.id.my_first_text_view, R.id.my_first_button, 1000);
setupViews(R.id.my_second_text_view, R.id.my_second_button, 2000);
setupViews(R.id.my_third_text_view, R.id.my_third_button, 3000);

然后根据ActivityResult处理requestCode(将现有更新代码移至名为updateTextView的方法):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1000) {
            updateTextView(R.id.my_first_text_view);
        } else if (requestCode == 2000) {
            updateTextView(R.id.my_second_text_view);
        } else if (requestCode == 3000) {
            updateTextView(R.id.my_third_text_view);
        }
    }
}