android studio电话簿代码
时间: 2023-07-07 19:43:41 浏览: 146
android自己的电话簿源代码
5星 · 资源好评率100%
很抱歉,由于电话簿应用程序包含大量的代码和文件,无法在这里提供完整的代码。但是,我可以给您提供一些关于如何创建电话簿应用程序的指导:
1. 创建一个新的Android Studio项目并添加必要的依赖库。
2. 创建一个数据模型类来表示联系人的信息,例如:
```
public class Contact {
private int id;
private String name;
private String phone;
// getters and setters
}
```
3. 使用SQLite数据库来存储联系人信息,例如:
```
public class ContactDatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
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";
public ContactDatabaseHelper(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.getPhone());
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
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.setPhone(cursor.getString(2));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, COLUMN_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
db.close();
}
}
```
4. 在布局文件中添加RecyclerView和CardView来显示联系人列表,例如:
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<androidx.cardview.widget.CardView
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="30dp"
app:cardElevation="6dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add_white_24dp" />
</androidx.cardview.widget.CardView>
```
5. 创建一个活动来显示联系人列表和添加新联系人,例如:
```
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ContactAdapter adapter;
private List<Contact> contactList = new ArrayList<>();
private ContactDatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
adapter = new ContactAdapter(this, contactList);
recyclerView.setAdapter(adapter);
db = new ContactDatabaseHelper(this);
contactList.addAll(db.getAllContacts());
adapter.notifyDataSetChanged();
}
public void addContact(View view) {
Intent intent = new Intent(this, AddContactActivity.class);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
contactList.clear();
contactList.addAll(db.getAllContacts());
adapter.notifyDataSetChanged();
}
}
```
6. 创建一个活动来添加新联系人,例如:
```
public class AddContactActivity extends AppCompatActivity {
private EditText inputName, inputPhone;
private ContactDatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
inputName = findViewById(R.id.inputName);
inputPhone = findViewById(R.id.inputPhone);
db = new ContactDatabaseHelper(this);
}
public void saveContact(View view) {
String name = inputName.getText().toString().trim();
String phone = inputPhone.getText().toString().trim();
if (!name.isEmpty() && !phone.isEmpty()) {
Contact contact = new Contact();
contact.setName(name);
contact.setPhone(phone);
db.addContact(contact);
finish();
} else {
Toast.makeText(this, "Please enter name and phone", Toast.LENGTH_SHORT).show();
}
}
}
```
7. 创建一个适配器类来绑定联系人列表中的数据,例如:
```
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyViewHolder> {
private Context context;
private List<Contact> contactList;
private ContactDatabaseHelper db;
public ContactAdapter(Context context, List<Contact> contactList) {
this.context = context;
this.contactList = contactList;
db = new ContactDatabaseHelper(context);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.contact_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
final Contact contact = contactList.get(position);
holder.name.setText(contact.getName());
holder.phone.setText(contact.getPhone());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, EditContactActivity.class);
intent.putExtra("id", contact.getId());
intent.putExtra("name", contact.getName());
intent.putExtra("phone", contact.getPhone());
context.startActivity(intent);
}
});
holder.call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + contact.getPhone()));
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, 1);
return;
}
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return contactList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, phone;
public ImageView call;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
phone = view.findViewById(R.id.phone);
call = view.findViewById(R.id.call);
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Contact contact = contactList.get(position);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + contact.getPhone()));
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, 1);
return;
}
context.startActivity(intent);
}
});
}
}
}
```
希望这些代码能对您有所帮助!
阅读全文