Android實(shí)現(xiàn)新增及編輯聯(lián)系人的方法
本文實(shí)例介紹了Android開發(fā)中對(duì)聯(lián)系人修改、新增聯(lián)系人的方法,通過本實(shí)例代碼可實(shí)現(xiàn)添加聯(lián)系人、編輯修改聯(lián)系人,新增聯(lián)系人和更新聯(lián)系人等操作,操作主要放在線程中處理,并且在操作的過程中,顯示圓形進(jìn)度條,在Android系統(tǒng)中,這是種比較常見的進(jìn)度條風(fēng)格。
具體功能代碼如下所示:
package huahua.contactsfragment; import java.util.Collections; import huahua.huahuacontacts.R; import huahua.huahuacontacts.Utils; import android.app.Activity; import android.app.ProgressDialog; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.RawContacts; import android.provider.ContactsContract.RawContacts.Data; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class AddContactsActivity extends Activity{ private Button m_SaveBtn; private EditText m_EditName; private EditText m_EditNum; private TextView m_TextTitle; private String m_ContactId; private int m_Type; ProgressDialog m_dialogLoading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.add_contacts); Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("person"); m_TextTitle = (TextView)findViewById(R.id.text_title); m_EditName = (EditText)findViewById(R.id.edit_name); m_EditNum = (EditText)findViewById(R.id.edit_num); m_Type = bundle.getInt("tpye"); m_EditName.setText(bundle.getString("name")); m_EditNum.setText(bundle.getString("number")); if(m_Type == 0)//新增聯(lián)系人 { m_TextTitle.setText("新增聯(lián)系人"); } else if(m_Type == 1)//編輯聯(lián)系人 { m_ContactId = bundle.getString("id"); m_TextTitle.setText("編輯聯(lián)系人"); } m_SaveBtn = (Button)findViewById(R.id.btn_save_contact); m_SaveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if("".equals(m_EditName.getText().toString())) { Toast.makeText(AddContactsActivity.this, "請(qǐng)輸入聯(lián)系人姓名", Toast.LENGTH_SHORT).show(); } else if("".equals(m_EditNum.getText().toString())) { Toast.makeText(AddContactsActivity.this, "請(qǐng)輸入聯(lián)系人電話", Toast.LENGTH_SHORT).show(); } else if(m_Type == 0) { //新增聯(lián)系人操作,放在線程中處理 new SaveContactTask().execute(); } else if(m_Type == 1) { //更新聯(lián)系人操作,放在線程中處理 new ChangeContactTask().execute(); } } }); } class SaveContactTask extends AsyncTask<Void, Integer, Void>{ @Override protected Void doInBackground(Void... params) { Utils.AddContact(m_EditName.getText().toString(), m_EditNum.getText().toString()); return null; } @Override protected void onPostExecute(Void result) { if(m_dialogLoading!= null) { m_dialogLoading.dismiss(); finish(); } } @Override protected void onPreExecute() { m_dialogLoading = new ProgressDialog(AddContactsActivity.this); m_dialogLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER);//設(shè)置風(fēng)格為圓形進(jìn)度條 m_dialogLoading.setMessage("正在保存聯(lián)系人"); m_dialogLoading.setCancelable(false); m_dialogLoading.show(); } @Override protected void onProgressUpdate(Integer... values) { } } class ChangeContactTask extends AsyncTask<Void, Integer, Void>{ @Override protected Void doInBackground(Void... params) { Utils.ChangeContact(m_EditName.getText().toString(), m_EditNum.getText().toString(),m_ContactId); return null; } @Override protected void onPostExecute(Void result) { if(m_dialogLoading!= null) { m_dialogLoading.dismiss(); finish(); } } @Override protected void onPreExecute() { m_dialogLoading = new ProgressDialog(AddContactsActivity.this); m_dialogLoading.setProgressStyle(ProgressDialog.STYLE_SPINNER);//設(shè)置風(fēng)格為圓形進(jìn)度條 m_dialogLoading.setMessage("正在保存聯(lián)系人"); m_dialogLoading.setCancelable(false); m_dialogLoading.show(); } @Override protected void onProgressUpdate(Integer... values) { } } }
- Android之聯(lián)系人PinnedHeaderListView使用介紹
- android 加載本地聯(lián)系人實(shí)現(xiàn)方法
- Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
- Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
- android實(shí)現(xiàn)讀取、搜索聯(lián)系人的代碼
- android獲取聯(lián)系人示例分享
- Android獲取手機(jī)通話記錄的方法
- Android通話記錄備份實(shí)現(xiàn)代碼
- Android SD卡上文件操作及記錄日志操作實(shí)例分析
- Android中刪除文件以及文件夾的命令記錄
- Android開發(fā)實(shí)現(xiàn)刪除聯(lián)系人通話記錄的方法
相關(guān)文章
Android窗口小部件基礎(chǔ)編寫代碼實(shí)例
這篇文章主要介紹了Android窗口小部件基礎(chǔ)編寫代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Android 中IntentFilter的匹配規(guī)則實(shí)例詳解
這篇文章主要介紹了Android 中IntentFilter的匹配規(guī)則實(shí)例詳解的相關(guān)資料,希望通過本文大家能了解掌握IntentFilter的匹配規(guī)則問題,需要的朋友可以參考下2017-09-09Android開發(fā)之日歷CalendarView用法示例
這篇文章主要介紹了Android開發(fā)之日歷CalendarView用法,簡(jiǎn)單分析了日歷CalendarView組件的功能、屬性設(shè)置方法、界面布局、事件監(jiān)聽等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03android listview初步學(xué)習(xí)實(shí)例代碼
這篇文章主要介紹了android listview初步學(xué)習(xí)實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01Android編程之滑動(dòng)按鈕事件實(shí)例詳解
這篇文章主要介紹了Android編程之滑動(dòng)按鈕事件,結(jié)合具體實(shí)例形式分析了Android滑動(dòng)按鈕功能的具體實(shí)現(xiàn)步驟、布局及功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-03-03