Android studio通讯录制作代码
时间: 2023-12-20 14:06:54 浏览: 161
好的,下面是一个简单的 Android Studio 通讯录制作代码示例,使用 SQLite 数据库来存储联系人信息:
1. 创建一个名为 "Contact" 的 Java 类,用于存储联系人信息。
```java
public class Contact {
private int id;
private String name;
private String phoneNumber;
public Contact() {}
public Contact(int id, String name, String phoneNumber) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
```
2. 创建一个名为 "DatabaseHelper" 的 Java 类,用于管理 SQLite 数据库。
```java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contacts.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CONTACTS = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_PHONE = "phone_number";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY," +
COLUMN_NAME + " TEXT," +
COLUMN_PHONE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
onCreate(db);
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_PHONE, contact.getPhoneNumber());
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
contactList.add(contact);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return contactList;
}
public void updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_PHONE, contact.getPhoneNumber());
db.update(TABLE_CONTACTS, values, COLUMN_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
db.close();
}
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, COLUMN_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
db.close();
}
}
```
3. 在布局文件中添加一个 RecyclerView 和一个 FloatingActionButton。
```xml
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@android:drawable/ic_input_add"/>
```
4. 在 MainActivity 类中实现 RecyclerView 和 FloatingActionButton 的逻辑。
```java
public class MainActivity extends AppCompatActivity {
private List<Contact> contactList = new ArrayList<>();
private RecyclerView recyclerView;
private ContactAdapter adapter;
private DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
adapter = new ContactAdapter(contactList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
db = new DatabaseHelper(this);
contactList.addAll(db.getAllContacts());
adapter.notifyDataSetChanged();
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddContactActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Contact contact = (Contact) data.getSerializableExtra("contact");
db.addContact(contact);
contactList.clear();
contactList.addAll(db.getAllContacts());
adapter.notifyDataSetChanged();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
}
}
```
5. 在 AddContactActivity 类中实现添加联系人的逻辑。
```java
public class AddContactActivity extends AppCompatActivity {
private EditText inputName;
private EditText inputPhoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
inputName = findViewById(R.id.input_name);
inputPhoneNumber = findViewById(R.id.input_phone_number);
Button btnSave = findViewById(R.id.btn_save);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = inputName.getText().toString();
String phoneNumber = inputPhoneNumber.getText().toString();
if (!name.isEmpty() && !phoneNumber.isEmpty()) {
Contact contact = new Contact();
contact.setName(name);
contact.setPhoneNumber(phoneNumber);
Intent intent = new Intent();
intent.putExtra("contact", contact);
setResult(RESULT_OK, intent);
finish();
}
}
});
}
}
```
这样一个简单的 Android Studio 通讯录制作代码就完成了。当用户点击 FloatingActionButton 时,将跳转到 AddContactActivity 界面,用户可以输入联系人的姓名和电话号码,并保存到 SQLite 数据库中。在 MainActivity 中使用 RecyclerView 显示所有保存的联系人信息。
阅读全文