Androidstudio开发,实现通讯录编辑/修改联系人( 五)

1. 涉及到的技术点

  1. SQLite的使用
  2. 列表控件ListView的使用
  3. ListView事件监听
  4. 适配器BaseAdapter的使用
  5. 线性布局LinearLayoutCompat的使用

2. 开发环境

  1. 开发工具:AndroidStudio
  2. 开发语言:java
  3. jdk版本:8.0+以上

3. 需求分析

在上两集中,已经实现了通讯录联系人的添加,并通过ListView控件,将通讯录联系人展示出来了,今天我们来实现对通讯录联系人的修改

4. 实现步骤

  1. 创建一个名叫EditStudentInfoActivity的活动页面
public class EditStudentInfoActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_student_info);
    }
}
  1. 编写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>

温馨提示:其实编辑页面跟添加联系人页面是一样的 效果图如下:
在这里插入图片描述

  1. 初始化控件
        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_mobile = findViewById(R.id.et_mobile);
        et_class_name = findViewById(R.id.et_class_name);
  1. 编辑/修改按钮点击事件
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

  • 25
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值