Android中的ContactPicker(選擇連絡人) 詳解

來源:互聯網
上載者:User

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選擇按鈕, 啟動介面;

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.