<!-- 读取联系人记录的权限 -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
activity_main<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/et_main_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入或选择一个联系人号码" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_main_number"
        android:text="选择联系人"
        android:onClick="toContactList"/>

</RelativeLayout>
item_contact<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/tv_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp"
        android:layout_marginLeft="10dp"/>

</LinearLayout>
activity_contact_list<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</ListView>
public class MainActivity extends Activity {

    private EditText et_main_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_main_number = (EditText) findViewById(R.id.et_main_number);
    }

    public void toContactList(View v) {
        //启动联系人列表界面
        startActivityForResult(new Intent(this, ContactListActivity.class), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1 && resultCode==RESULT_OK) {
            //得到返回的number
            String number = data.getStringExtra("NUMBER");
            //显示
            et_main_number.setText(number);
        }
    }
}
public class ContactListActivity extends ListActivity implements OnItemClickListener {

    private ListView listView;
    private ContactAdapter adapter;
    private List<Map<String, String>> data = new ArrayList<Map<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);

        listView = getListView();
        adapter = new ContactAdapter();
        //查询得到联系人表数据
            //ContentResolver
        ContentResolver resolver = getContentResolver();
            //执行查询得到cursor
        String[] projection = {Phone.DISPLAY_NAME, Phone.NUMBER};
        Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, null, null, null);
            //取出其中的数据保存到data
        while(cursor.moveToNext()) {
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", name);
            map.put("number", number);
            data.add(map);
        }
        //显示列表
        listView.setAdapter(adapter);

        //给listView添加item点击监听
        listView.setOnItemClickListener(this);

    }

    class ContactAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null) {
                convertView = View.inflate(ContactListActivity.this, R.layout.item_contact, null);
            }

            Map<String, String> map = data.get(position);
            TextView nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
            TextView nubmerTV = (TextView) convertView.findViewById(R.id.tv_item_number);
            nameTV.setText(map.get("name"));
            nubmerTV.setText(map.get("number"));

            return convertView;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //得到选择的号码
        String number = data.get(position).get("number");
        Intent intent = getIntent();
        intent.putExtra("NUMBER", number);
        //设置结果
        setResult(RESULT_OK, intent);
        //返回
        finish();
    }
}
05-11 22:49