1. 涉及到的技术点
- SQLite的使用
- 列表控件ListView的使用
- ListView事件监听
- 适配器BaseAdapter的使用
- 线性布局LinearLayoutCompat的使用
2. 开发环境
- 开发工具:AndroidStudio
- 开发语言:java
- jdk版本:8.0+以上
3. 需求分析
在上两集中,已经实现了通讯录联系人的添加,并通过ListView控件,将通讯录联系人展示出来了,今天我们来实现对通讯录联系人的修改
4. 实现步骤
- 创建一个名叫EditStudentInfoActivity的活动页面
public class EditStudentInfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_student_info);
}
}
- 编写activity_edit_student_info.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AddStudentInfoActivity">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_500"
app:title="编辑"
app:navigationIcon="@mipmap/img_back"
app:titleTextColor="@color/white">
</androidx.appcompat.widget.Toolbar>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
app:cardBackgroundColor="#f5f5f5"
app:cardCornerRadius="4dp"
app:cardElevation="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="姓名:"
android:textColor="#222222" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:background="@null"
android:hint="请输入姓名"
android:textSize="14sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#f5f5f5"
app:cardCornerRadius="4dp"
app:cardElevation="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="手机号:"
android:textColor="#222222" />
<EditText
android:id="@+id/et_mobile"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:background="@null"
android:hint="请输入手机号"
android:textSize="14sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
app:cardBackgroundColor="#f5f5f5"
app:cardCornerRadius="4dp"
app:cardElevation="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="班级:"
android:textColor="#222222" />
<EditText
android:id="@+id/et_class_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:background="@null"
android:hint="请输入班级"
android:textSize="14sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="4dp"
app:cardElevation="0dp">
<TextView
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500"
android:gravity="center"
android:text="编辑"
android:textColor="@color/white" />
</androidx.cardview.widget.CardView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
温馨提示:其实编辑页面跟添加联系人页面是一样的
效果图如下:
- 初始化控件
//初始化控件
et_username = findViewById(R.id.et_username);
et_mobile = findViewById(R.id.et_mobile);
et_class_name = findViewById(R.id.et_class_name);
- 编辑/修改按钮点击事件
findViewById(R.id.edit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = et_username.getText().toString();
String mobile = et_mobile.getText().toString();
String class_name = et_class_name.getText().toString();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(mobile) || TextUtils.isEmpty(class_name)) {
Toast.makeText(EditStudentInfoActivity.this, "请完善信息", Toast.LENGTH_SHORT).show();
}else {
//初始化数据库连接
StudentDb studentDb = StudentDb.getInstance(EditStudentInfoActivity.this);
studentInfo.setMobile(mobile);
studentInfo.setClass_name(class_name);
studentInfo.setUsername(username);
studentDb.editInfo(studentInfo);
Toast.makeText(EditStudentInfoActivity.this, "需改成功", Toast.LENGTH_SHORT).show();
//设置回传刷新数据
setResult(1000);
finish();
}
}
});
5. 代码实现全部过程
/**
* 修改信息页面
*/
public class EditStudentInfoActivity extends AppCompatActivity {
private EditText et_username;
private EditText et_mobile;
private EditText et_class_name;
private StudentInfo studentInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_student_info);
//获取跳转传递的参数
studentInfo = (StudentInfo) getIntent().getSerializableExtra("studentInfo");
//初始化控件
et_username = findViewById(R.id.et_username);
et_mobile = findViewById(R.id.et_mobile);
et_class_name = findViewById(R.id.et_class_name);
//设置数据
et_mobile.setText(studentInfo.getMobile());
et_username.setText(studentInfo.getUsername());
et_class_name.setText(studentInfo.getClass_name());
//编辑
findViewById(R.id.edit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = et_username.getText().toString();
String mobile = et_mobile.getText().toString();
String class_name = et_class_name.getText().toString();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(mobile) || TextUtils.isEmpty(class_name)) {
Toast.makeText(EditStudentInfoActivity.this, "请完善信息", Toast.LENGTH_SHORT).show();
}else {
//初始化数据库连接
StudentDb studentDb = StudentDb.getInstance(EditStudentInfoActivity.this);
studentInfo.setMobile(mobile);
studentInfo.setClass_name(class_name);
studentInfo.setUsername(username);
studentDb.editInfo(studentInfo);
Toast.makeText(EditStudentInfoActivity.this, "需改成功", Toast.LENGTH_SHORT).show();
//设置回传刷新数据
setResult(1000);
finish();
}
}
});
}
}
注意事项:
studentInfo = (StudentInfo) getIntent().getSerializableExtra("studentInfo");
是从通讯录列表页,通过listview的点击事件跳转传值到编辑界面,因为编辑页面默认是要显示被修改的通讯录联系人信息
6. 其它学习资料
温馨提示:通讯录的主页是BottomNavigationView底部导航栏的形式
,底部导航栏的案例,本人出了教程视频,这里就忽略掉这步的实现过程
1. Androidstudio底部导航栏实现: https://www.bilibili.com/video/BV1XB4y1d7et/?spm_id_from=333.337.search-card.all.click&vd_source=984bb03f768809c7d33f20179343d8c8