編輯:關於Android編程
ContactPicker:用戶可以選擇一個聯系人.
位置: 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;
位置: res->layout->activity_main.xml
位置: res->layout->listitemlayout.xml
位置: res->layout->contactpickertester.xml
位置: 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; } } }
2. 在onClick()方法中, 指定Intent.ACTION_PICK動作, 和聯系人數據庫(content://contacts/);
3. 重寫onActivityResult()事件, 處理動作返回值, reqCode, 請求碼, resCode, 結果碼, data, Intent數據;
4. 提取數據, 更新ViewText的內容;
位置: root->AndroidManifest.xml
2. 注冊ContactPickerTester, 啟動(MAIN)程序;
代碼下載: http://download.csdn.net/detail/u012515223/7072781
簡介:PullToRefresh是一款支持ListView,GridView,ViewPager,ScrollView,WebView等一切可以拖動,並實現
GLSurfaceView提供了下列特性:1> 管理一個surface,這個surface就是一塊特殊的內存,能直接排版到android的視圖view上。2>
IntentService一、IntentService概述??上一篇我們聊到了HandlerThread,本篇我們就來看看HandlerThread在IntentSer
用過新版本android 360手機助手都人都對 360中只在桌面顯示一個小小懸浮窗口羨慕不已吧? 其實實現這種功能,主要有兩步: 1.判斷當前顯示的是為桌面。這個內容我