本文实例介绍了Android开发中对联系人修改、新增联系人的方法,通过本实例代码可实现添加联系人、编辑修改联系人,新增联系人和更新联系人等操作,操作主要放在线程中处理,并且在操作的过程中,显示圆形进度条,在Android系统中,这是种比较常见的进度条风格。
具体功能代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
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 ) //新增联系人 { m_TextTitle.setText( "新增联系人" ); } else if (m_Type == 1 ) //编辑联系人 { m_ContactId = bundle.getString( "id" ); m_TextTitle.setText( "编辑联系人" ); } 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 , "请输入联系人姓名" , Toast.LENGTH_SHORT).show(); } else if ( "" .equals(m_EditNum.getText().toString())) { Toast.makeText(AddContactsActivity. this , "请输入联系人电话" , Toast.LENGTH_SHORT).show(); } else if (m_Type == 0 ) { //新增联系人操作,放在线程中处理 new SaveContactTask().execute(); } else if (m_Type == 1 ) { //更新联系人操作,放在线程中处理 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); //设置风格为圆形进度条 m_dialogLoading.setMessage( "正在保存联系人" ); 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); //设置风格为圆形进度条 m_dialogLoading.setMessage( "正在保存联系人" ); m_dialogLoading.setCancelable( false ); m_dialogLoading.show(); } @Override protected void onProgressUpdate(Integer... values) { } } } |