中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android - ContactPicker(選擇聯系人) 詳解

發布時間:2020-08-06 08:53:19 來源:網絡 閱讀:376 作者:morndragon 欄目:移動開發

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(選擇聯系人) 詳解

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宜宾市| 奉贤区| 平陆县| 永德县| 嘉义县| 天柱县| 凤阳县| 永靖县| 黄骅市| 平度市| 兰州市| 新兴县| 镇原县| 双江| 丰台区| 柘荣县| 靖西县| 昆明市| 华宁县| 荔浦县| 津市市| 郸城县| 无为县| 三江| 甘孜县| 措美县| 石棉县| 南京市| 高邑县| 璧山县| 旌德县| 定西市| 兖州市| 怀安县| 垫江县| 西和县| 辽宁省| 沐川县| 图木舒克市| 南城县| 监利县|