Android - ContactPicker(选择联系人) 详解

ContactPicker(选择联系人) 详解


本文地址: http://blog.csdn.net/caroline_wendy/article/details/21629357


Android API: http://developer.android.com/reference/android/content/Intent.html


ContactPicker:用户可以选择一个联系人.


1. 创建ContactPicker活动(Activity)

位置: java->package-> ContactPicker.java

package mzx.spike.contactpicker.app;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * Created by C.L.Wang on 14-3-20.
 */
public class ContactPicker extends Activity {

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

        final Cursor c = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null
        );

        String[] from = new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY};
        int[] to = new int[]{R.id.itemTextView};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listitemlayout, c, from, to, 0);
        ListView lv = (ListView)findViewById(R.id.contactListView);

        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new ListView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                c.moveToPosition(pos);
                int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
                Uri outURI = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId);
                Intent outData = new Intent();
                outData.setData(outURI);
                setResult(Activity.RESULT_OK, outData);
                finish();
            }
        });
    }
}

详解:

1.  创建一个活动继承Activity, 显示activity_main界面;

2. 创建游标Cursor遍历存储在联系人列表中的联系人;

3. 构造简单游标适配器(SimpleCursorAdapter), 注意此方法标记为遗弃, 后面需要加上参数0, 版本SDK11+;

4. 将适配器绑定至ListView, setAdapter();

5. 添加项目点击事件(setOnItemClickListener), 将游标移植点击位置, 取出rowID, 构造URI, 将URI绑定至Intent,;

6. 返回结果, setResult(), 结果码(Activity_RESULT_OK) 和 Intent;


2. 修改activity_main.xml

位置: res->layout->activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
>

    <ListView android:id="@+id/contactListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />

</RelativeLayout>

添加ListView, ContactPicker会调用此布局(layout);


3. 创建listitemlayout.xml

位置: res->layout->listitemlayout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/itemTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16dp"
        android:textColor="#F0F"
    />

</LinearLayout>

TextView显示ListView中的每一个联系人, ContactPicker会调用此布局(layout);


4. 创建测试布局contactpickertester.xml

位置: res->layout->contactpickertester.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/selected_contact_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />

    <Button
        android:id="@+id/pick_contact_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pick Contact"
    />

</LinearLayout>

TextView显示选择的联系人,Button选择按钮, 启动界面;


5. 创建测试类ContactPickerTester.java

位置: java->package->ContactPickerTester.java

package mzx.spike.contactpicker.app;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Administrator on 14-3-20.
 */
public class ContactPickerTester extends Activity {

    public static final int PICK_CONTACT = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.contactpickertester);
        Button button = (Button)findViewById(R.id.pick_contact_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/"));
                startActivityForResult(intent, PICK_CONTACT);
            }
        });
    }

    @Override
    public void onActivityResult(int reqCode, int resCode, Intent data) {
        super.onActivityResult(reqCode, resCode, data);

        switch (reqCode) {
            case (PICK_CONTACT) : {
                if (resCode == Activity.RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor c = getContentResolver().query(contactData,null, null, null, null);
                    c.moveToFirst();
                    String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                    c.close();
                    TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
                    tv.setText(name);
                }
            }
            default: break;
        }
    }

}

1. 在onCreate()方法内, 重写按键监听事件(button.setOnClickListene());

2. 在onClick()方法中, 指定Intent.ACTION_PICK动作, 和联系人数据库("content://contacts/");

3. 重写onActivityResult()事件, 处理动作返回值, reqCode, 请求码, resCode, 结果码,  data, Intent数据;

4. 提取数据, 更新ViewText的内容;


6. 修改Manifest, 进行注册

位置: root->AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mzx.spike.contactpicker.app" >

    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ContactPicker"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.PICK" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:path="contacts" android:scheme="content"/>
            </intent-filter>
        </activity>

        <activity android:name=".ContactPickerTester"
            android:label="Contact Picker Test">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
    </application>

</manifest>

1. 注册ContactPicker, intent-filter, 动作PICK, 种类DEFAULT(默认动作);

2. 注册ContactPickerTester, 启动(MAIN)程序;


7. 运行程序


代码下载: http://download.csdn.net/detail/u012515223/7072781


Android - ContactPicker(选择联系人) 详解

Android - ContactPicker(选择联系人) 详解,布布扣,bubuko.com

Android - ContactPicker(选择联系人) 详解

上一篇:android 快捷键


下一篇:[转]使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 事件详解