1

通过使用此代码,我可以访问联系人列表,但是当我单击联系人时,它没有显示在我的编辑文本框中

这是我的java代码

    package com.example.test;        
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.Contacts;
    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

这是 MainActivity 和 pc 是一个编辑文本框,我想在其中显示电话号码

    public class MainActivity extends Activity {

        @SuppressWarnings("unused")
        private static final int PICK_CONTACT_REQUEST = 0;

        private static final int PICK_CONTACT = 1;
        Button pickContact;
        EditText pc;
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pickContact = (Button) findViewById(R.id.button1);
            pc = (EditText) findViewById(R.id.editText1);
            pickContact.setOnClickListener(new OnClickListener()


        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            pickContact = (Button) findViewById(R.id.button1);
            pc = (EditText) findViewById(R.id.editText1);
            pickContact.setOnClickListener(new OnClickListener() 
            {

我不确定此意图功能是否正确,尽管它使我可以访问联系方式

               public void onClick(View arg0) {
                    Intent intent1 = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
                    startActivityForResult(intent1, PICK_CONTACT);
                    startActivity(intent1);
               }
            }
            );

        }

这是我觉得不对劲的地方,因为它没有显示在editText框中

        public void onActivityResult(int reqCode, int resultCode, Intent data) { 
            super.onActivityResult(reqCode, resultCode, data); 

            switch (reqCode) { 
              case (PICK_CONTACT) : 
                if (resultCode == Activity.RESULT_OK) { 
                    Uri contactData = data.getData(); 
                      Cursor c = getContentResolver().query(contactData, null, null, null, null); 
                      if (c.moveToPosition(PICK_CONTACT)) { 
                         String name=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                          String number=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(this,  name + " has number " + number, Toast.LENGTH_LONG).show(); 
                         pc.setText(number);
                         pc.setText(name);
                      } 
                 } 

               break; 
        }    
     }
  }

谢谢 :)

4

0 回答 0