如何通过按android中的添加按钮显示电话号码

时间:2013-11-06 14:57:58

标签: android

通过使用此代码,我可以访问联系人列表,但是当我点击其未显示到我的编辑文本框中的联系人

这是我的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是一个editText框,我想显示电话号码

    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; 
        }    
     }
  }

谢谢:)

0 个答案:

没有答案