用Android studio写一个通讯录实现三个页面的跳转
时间: 2024-06-11 17:04:06 浏览: 157
1. 首先,创建一个新的Android Studio项目。
2. 在项目中创建三个Activity,分别为ContactsActivity、AddContactActivity和EditContactActivity。
3. 在ContactsActivity中,可以显示所有的联系人列表,并提供一个按钮用于添加新的联系人。当用户点击添加按钮时,应该跳转到AddContactActivity页面。
4. 在AddContactActivity中,用户可以输入新的联系人的信息,并保存这些信息。当用户保存联系人信息时,应该返回到ContactsActivity页面,并刷新联系人列表。
5. 在ContactsActivity中,每个联系人都应该有一个编辑按钮。当用户点击编辑按钮时,应该跳转到EditContactActivity页面,允许用户编辑该联系人的详细信息。
6. 在EditContactActivity中,用户可以编辑联系人的详细信息,并保存这些信息。当用户保存联系人信息时,应该返回到ContactsActivity页面,并刷新联系人列表。
7. 在AndroidManifest.xml中配置三个Activity的跳转。
示例代码:
ContactsActivity.java
```
public class ContactsActivity extends AppCompatActivity {
private Button addButton;
private ListView contactsListView;
private ArrayList<Contact> contactsList;
private ContactsAdapter contactsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
addButton = findViewById(R.id.add_button);
contactsListView = findViewById(R.id.contacts_list_view);
contactsList = new ArrayList<>();
contactsAdapter = new ContactsAdapter(this, contactsList);
contactsListView.setAdapter(contactsAdapter);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ContactsActivity.this, AddContactActivity.class);
startActivityForResult(intent, 1);
}
});
contactsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(ContactsActivity.this, EditContactActivity.class);
intent.putExtra("contact", contactsList.get(i));
startActivityForResult(intent, 2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Contact contact = (Contact) data.getSerializableExtra("contact");
contactsList.add(contact);
contactsAdapter.notifyDataSetChanged();
} else if (requestCode == 2) {
Contact contact = (Contact) data.getSerializableExtra("contact");
int position = data.getIntExtra("position", -1);
if (position != -1) {
contactsList.set(position, contact);
contactsAdapter.notifyDataSetChanged();
}
}
}
}
}
```
AddContactActivity.java
```
public class AddContactActivity extends AppCompatActivity {
private Button saveButton;
private EditText nameEditText;
private EditText phoneEditText;
private EditText emailEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
saveButton = findViewById(R.id.save_button);
nameEditText = findViewById(R.id.name_edit_text);
phoneEditText = findViewById(R.id.phone_edit_text);
emailEditText = findViewById(R.id.email_edit_text);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
Contact contact = new Contact(name, phone, email);
Intent intent = new Intent();
intent.putExtra("contact", contact);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
EditContactActivity.java
```
public class EditContactActivity extends AppCompatActivity {
private Button saveButton;
private EditText nameEditText;
private EditText phoneEditText;
private EditText emailEditText;
private Contact contact;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_contact);
saveButton = findViewById(R.id.save_button);
nameEditText = findViewById(R.id.name_edit_text);
phoneEditText = findViewById(R.id.phone_edit_text);
emailEditText = findViewById(R.id.email_edit_text);
contact = (Contact) getIntent().getSerializableExtra("contact");
position = getIntent().getIntExtra("position", -1);
if (contact != null) {
nameEditText.setText(contact.getName());
phoneEditText.setText(contact.getPhone());
emailEditText.setText(contact.getEmail());
}
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
Contact updatedContact = new Contact(name, phone, email);
Intent intent = new Intent();
intent.putExtra("contact", updatedContact);
intent.putExtra("position", position);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
AndroidManifest.xml
```
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contacts">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ContactsActivity" />
<activity android:name=".AddContactActivity" />
<activity android:name=".EditContactActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
```
阅读全文