多个EditText字段,添加联系人按钮。

时间:2014-02-06 15:11:38

标签: android android-edittext

我正在尝试从电话簿中获取联系人并将号码放在editText中。我想在同一个活动中做4次这个动作。所以每个editText都有自己的添加按钮。我创建了一个onActivityresult来执行此操作,但它不起作用。有人可以帮我解释一下这段代码吗?

Intent contactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent intent) 
{
    super.onActivityResult(reqCode, resultCode, intent);
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData1 = intent.getData();
         Cursor c1 = getContentResolver().query(contactData1, null, null, null, null);
         c1.moveToFirst();
         String cNumber1 = c1.getString(c1.getColumnIndex("data1"));
         TextView tv1;
         tv1 = (TextView)findViewById(R.id.number1);
         tv1.setText(cNumber1);
    }
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData2 = intent.getData();
         Cursor c2 = getContentResolver().query(contactData2, null, null, null, null);
         c2.moveToFirst();
         String cNumber2 = c2.getString(c2.getColumnIndex("data1"));
         TextView tv2;
         tv2 = (TextView)findViewById(R.id.number2);
         tv2.setText(cNumber2);
    }
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData3 = intent.getData();
         Cursor c3 = getContentResolver().query(contactData3, null, null, null, null);
         c3.moveToFirst();
         String cNumber3 = c3.getString(c3.getColumnIndex("data1"));
         TextView tv3;
         tv3 = (TextView)findViewById(R.id.number3);
         tv3.setText(cNumber3);
    }
    if (resultCode == Activity.RESULT_OK) 
    {
         Uri contactData4 = intent.getData();
         Cursor c4 = getContentResolver().query(contactData4, null, null, null, null);
         c4.moveToFirst();
         String cNumber = c4.getString(c4.getColumnIndex("data1"));
         TextView tv4;
         tv4 = (TextView)findViewById(R.id.number4);
         tv4.setText(cNumber);
    }


}

2 个答案:

答案 0 :(得分:0)

究竟什么不起作用?

你做了4次同样的事情,你可以在这里看得更清楚:

if (resultCode == Activity.RESULT_OK) 
{
     Uri contactData = intent.getData();
     Cursor cursor = getContentResolver().query(contactData1, null, null, null, null);
     cursor.moveToFirst();
     String contactNumber = cursor.getString(cursor.getColumnIndex("data1"));

     TextView tv1 = (TextView)findViewById(R.id.number1);
     tv1.setText(contactNumber);
     TextView tv2 = (TextView)findViewById(R.id.number2);
     tv2.setText(contactNumber);
     TextView tv3 = (TextView)findViewById(R.id.number3);
     tv3.setText(contactNumber);
     TextView tv4 = (TextView)findViewById(R.id.number4);
     tv4.setText(contactNumber);
}

我只是缩短了它。它实际上是相同的代码。 这样,每个TextView都会有相同的电话号码。

编辑:

这可能是一个解决方案。

要区分几个TextView,您应该声明一些final request constants

public static final int CONTACT_PICKER_REQUEST_FIRST = 0, CONTACT_PICKER_REQUEST_SECOND = 1, CONTACT_PICKER_REQUEST_THIRD = 2, CONTACT_PICKER_REQUEST_FOURTH = 3;

在按钮的onClick方法中输入如下内容:

public void onClick(View v) {
    int request_code = -1;

    switch(v.getId()) {
        case R.id.number1:
            request_code= CONTACT_PICKER_RESULT_FIRST;
            break;
        case R.id.number2:
            request_code= CONTACT_PICKER_RESULT_SECOND;
            break;
        case R.id.number3:
            request_code= CONTACT_PICKER_RESULT_THIRD;
            break;
        case R.id.number4:
            request_code= CONTACT_PICKER_RESULT_FOURTH;
            break;
    }

     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_UR);
    startActivityForResult(contactPickerIntent, request_code);
}

最后在你的onActivityResult()这样的事情应该有效:

protected void onActivityResult(int requestCode,int resultCode,Intent data){         if(resultCode == RESULT_OK){             TextView toFill;

        switch (requestCode) {
        case CONTACT_PICKER_REQUEST_SECOND:
            toFill = (TextView) findViewById(R.id.number2);
            break;
        case CONTACT_PICKER_REQUEST_THIRD:
            toFill = (TextView) findViewById(R.id.number3);
            break;
        case CONTACT_PICKER_REQUEST_FOURTH:
            toFill = (TextView) findViewById(R.id.number4);
            break;
        default:
            toFill = (TextView) findViewById(R.id.number1);
            break;
        }

        Uri uri = data.getData();
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        toFill.setText(phoneNumber);
    }
}

可能会有一些拼写错误,因为我在这里写下来而不是在我的ADT中。

答案 1 :(得分:0)

只是一个更新,@ Endzeit给了我很大的帮助。在我的例子中,我有4个EditTexts来填充。每个EditText都有自己的getContact按钮。所以我决定与那些有这种情况的人分享我的工作代码:

protected void onActivityResult(int reqCode, int resultCode, Intent intent) 
{
    super.onActivityResult(reqCode, resultCode, intent);
    switch (reqCode) {
    case ADDRESS_REQUEST2:
        EditText edittext2 = (EditText) findViewById(R.id.number2);
        Uri uri2 = intent.getData();
        Cursor cursor2 = getContentResolver().query(uri2, null, null, null, null);
        cursor2.moveToFirst();
        String phoneNumber2 = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext2.setText(phoneNumber2);
        break;
    case ADDRESS_REQUEST3:
        EditText edittext3 = (EditText) findViewById(R.id.number3);
        Uri uri3 = intent.getData();
        Cursor cursor3 = getContentResolver().query(uri3, null, null, null, null);
        cursor3.moveToFirst();
        String phoneNumber3 = cursor3.getString(cursor3.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext3.setText(phoneNumber3);
        break;
    case ADDRESS_REQUEST4:
        EditText edittext4 = (EditText) findViewById(R.id.number4);
        Uri uri4 = intent.getData();
        Cursor cursor4 = getContentResolver().query(uri4, null, null, null, null);
        cursor4.moveToFirst();
        String phoneNumber4 = cursor4.getString(cursor4.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext4.setText(phoneNumber4);
        break;
    default:
        EditText edittext1 = (EditText) findViewById(R.id.number1);
        Uri uri1 = intent.getData();
        Cursor cursor1 = getContentResolver().query(uri1, null, null, null, null);
        cursor1.moveToFirst();
        String phoneNumber1 = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        edittext1.setText(phoneNumber1);
        break;
    }