基于Android Studio 多功能记事本-MySQL版

目录

一、项目演示

二、开发环境

三、项目详情

四、项目完整源码


一、项目演示

基于Android Studio 多功能记事本--MySQL版

二、开发环境

三、项目详情

1.启动页

这段代码主要实现了以下功能:

1. **延迟跳转**:在 `StartActivity` 中,使用 `Handler` 的 `postDelayed` 方法延迟三秒后执行 `runnable`,跳转到 `MainActivity`。

2. **计时器**:使用 `CountDownTimer` 初始化一个四秒倒计时的计时器,每秒触发一次 `onTick` 方法,但不做任何操作。计时器结束时,会移除 `runnable`,确保跳转到 `MainActivity`。

总的来说,该代码在 `StartActivity` 启动后,会等待三秒后跳转到 `MainActivity`,同时设置了一个四秒的计时器来管理跳转的时机。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#fff"
    tools:context=".Activity.StartActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />
</androidx.constraintlayout.widget.ConstraintLayout>

2.登陆、注册

 private void login() {
        btnLogin.setOnClickListener(v -> {
            String phone = etPhone.getText().toString();
            String password = etPassword.getText().toString();
            if (phone.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "请输入完整的信息!", Toast.LENGTH_SHORT).show();
                return;
            }
            executorService.execute(() -> {
                boolean success = userDao.verifyUser(phone, password);
                runOnUiThread(() -> {
                    if (success) {
                        SharedPreferences sharedPreferences = getSharedPreferences("User", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("phone", phone);
                        editor.apply();

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "登录失败,请联系客服!", Toast.LENGTH_SHORT).show();
                    }
                });
            });
        });
    }

这段代码实现了登录功能,主要包括以下几个步骤:

1. **设置点击事件**:为登录按钮 `btnLogin` 设置点击事件监听器。

2. **获取输入信息**:从输入框中获取手机号码 (`etPhone`) 和密码 (`etPassword`)。

3. **信息校验**:检查手机号码和密码是否为空。如果任一为空,显示提示信息“请输入完整的信息!”并退出方法。

4. **后台验证**:
   - 使用 `executorService` 在后台线程中执行用户验证。
   - 调用 `userDao.verifyUser(phone, password)` 方法验证用户的手机号码和密码。

5. **更新UI**:
   - 在主线程中处理验证结果:
     - 如果验证成功,保存用户的手机号码到 `SharedPreferences` 中,并启动 `MainActivity`。显示“登录成功!”的提示消息,然后关闭当前 `LoginActivity`。
     - 如果验证失败,显示“登录失败,请联系客服!”的提示消息。

总结来说,这段代码实现了在用户点击登录按钮后,验证用户的手机号码和密码,并根据验证结果进行相应的提示和页面跳转。

    private void register() {
        btnRegister.setOnClickListener(v -> {
            String phone = etPhone.getText().toString();
            String name = etUserName.getText().toString();
            String password = etPassword.getText().toString();
            String rpassword = rPasswordEdittext.getText().toString();

            if (phone.isEmpty() || name.isEmpty() || password.isEmpty() || rpassword.isEmpty()) {
                Toast.makeText(this, "请确保所有内容都不为空!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!password.equals(rpassword)) {
                Toast.makeText(this, "两次密码不一致,请重新输入!", Toast.LENGTH_SHORT).show();
                return;
            }

            // 检查密码长度
            if (password.length() < 6 || password.length() > 16) {
                Toast.makeText(this, "密码长度必须在 6 到 16 位之间!", Toast.LENGTH_SHORT).show();
                return;
            }


            // 创建线程池并执行注册操作
            executorService.execute(() -> {
                boolean success = userDao.addUser(phone, name, password);
                runOnUiThread(() -> {
                    if (success) {
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(RegisterActivity.this, "注册失败,请联系客服!", Toast.LENGTH_SHORT).show();
                    }
                });
            });

        });
    }

这段代码实现了用户注册功能,主要包括以下几个步骤:

1. **设置点击事件**:为注册按钮 `btnRegister` 设置点击事件监听器。

2. **获取输入信息**:从输入框中获取手机号码 (`etPhone`)、用户名 (`etUserName`)、密码 (`etPassword`)、和确认密码 (`rPasswordEdittext`)。

3. **信息校验**:
   - 检查所有输入项是否为空。如果有为空项,显示“请确保所有内容都不为空!”的提示信息并退出方法。
   - 检查密码和确认密码是否一致。如果不一致,显示“两次密码不一致,请重新输入!”的提示信息并退出方法。
   - 检查密码长度是否在 6 到 16 位之间。如果不符合要求,显示“密码长度必须在 6 到 16 位之间!”的提示信息并退出方法。

4. **后台注册**:
   - 使用 `executorService` 在后台线程中执行用户注册操作,通过调用 `userDao.addUser(phone, name, password)` 方法来注册用户。

5. **更新UI**:
   - 在主线程中处理注册结果:
     - 如果注册成功,启动 `LoginActivity` 并显示“注册成功!”的提示消息,然后关闭当前 `RegisterActivity`。
     - 如果注册失败,显示“注册失败,请联系客服!”的提示消息。

总结来说,这段代码实现了用户在点击注册按钮后进行数据校验,并在后台进行注册操作,根据操作结果显示相应的提示并处理页面跳转。

3.笔记页、添加修改页

    private void show() {
        // 创建线程池并执行注册操作
        executor.execute(() -> {
            List<NoteBean> allNotesByUserPhone = noteDao.getAllNotesByUserPhone(phone);
            if (allNotesByUserPhone != null && !allNotesByUserPhone.isEmpty()) {
                // 对笔记列表进行排序
                Collections.sort(allNotesByUserPhone, new Comparator<NoteBean>() {
                    @Override
                    public int compare(NoteBean note1, NoteBean note2) {
                        SimpleDateFormat sdfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                        try {
                            Date date1 = sdfFull.parse(note1.getTime());
                            Date date2 = sdfFull.parse(note2.getTime());
                            if (date1 != null && date2 != null) {
                                return date2.compareTo(date1); // 按时间降序排序
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        return 0;
                    }
                });
            }

            getActivity().runOnUiThread(() -> {
                if (allNotesByUserPhone != null && !allNotesByUserPhone.isEmpty()) {
                    // 隐藏 TextView,显示 ListView
                    tvShow.setVisibility(View.GONE);
                    lv.setVisibility(View.VISIBLE);
                    // 创建适配器并设置到 ListView
                    NoteAdapter adapter = new NoteAdapter(getActivity(), allNotesByUserPhone);
                    lv.setAdapter(adapter);
                } else {
                    // 显示 TextView,隐藏 ListView
                    tvShow.setVisibility(View.VISIBLE);
                    lv.setVisibility(View.GONE);
                }
            });
        });
    }

这段代码实现了以下功能:

1. 从后台线程中获取用户的所有笔记。
2. 按笔记时间进行降序排序。
3. 在主线程中根据笔记列表是否为空更新UI:
   - 如果笔记存在,隐藏 `TextView` 并显示 `ListView`,将笔记显示在 `ListView` 中。
   - 如果笔记为空,显示 `TextView` 并隐藏 `ListView`。

1. **获取和处理 Note ID**:
   - 从 `Intent` 中获取 `note_id`。
   - 根据 `note_id` 判断是新建笔记还是编辑已有笔记。

2. **新建笔记功能**:
   - 当 `noteId` 为 -1 时,执行新建笔记的操作。
   - 从用户输入中获取标题、内容、字体颜色、字体大小等信息。
   - 格式化当前时间。
   - 如果标题或内容为空,显示提示信息。
   - 使用后台线程将笔记数据保存到数据库。
   - 保存成功后显示提示信息并关闭当前界面。

3. **编辑笔记功能**:
   - 当 `noteId` 有效时,加载并显示对应的笔记内容。
   - 从数据库中获取笔记信息,并更新到界面上。
   - 处理图片显示:将图片从 Base64 解码并显示,或隐藏图片视图。
   - 设置保存按钮的点击事件,用于更新笔记信息。
   - 更新成功后重新加载笔记数据,确保图片也更新,然后显示成功提示信息并关闭当前界面。

4. **异常处理**:
   - 处理图片解码和显示中的异常情况,确保应用稳定运行。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#F5F5F5"
    tools:context=".Activity.NoteActivity">

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:gravity="center|left"
        android:orientation="horizontal"
        android:padding="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/img_back"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="10dp"
            app:srcCompat="@drawable/back" />

        <ImageView
            android:id="@+id/img_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            app:srcCompat="@drawable/save" />
    </LinearLayout>

    <View
        android:id="@+id/view10"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#ccc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout4" />

    <ScrollView
        android:id="@+id/scrollView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="none"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view10"
        app:layout_constraintVertical_bias="1.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:orientation="vertical"
                android:padding="5dp">

                <EditText
                    android:id="@+id/et_title"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="#F5F5F5"
                    android:ems="10"
                    android:hint="点击输入标题"
                    android:inputType="textPersonName"
                    android:padding="4dp"
                    android:textColor="#000"
                    android:textColorHint="#9A9898"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <EditText
                    android:id="@+id/et_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="#F5F5F5"
                    android:ems="10"
                    android:gravity="top|left"
                    android:hint="开始你的书写吧~"
                    android:inputType="textMultiLine"
                    android:padding="10dp"
                    android:textColor="#000"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <ImageView
                    android:id="@+id/img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:visibility="gone"
                    tools:srcCompat="@tools:sample/avatars" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <View
        android:id="@+id/view11"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_marginBottom="2dp"
        android:background="#ccc"
        app:layout_constraintBottom_toBottomOf="@+id/scrollView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:orientation="horizontal"
        android:padding="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageView
            android:id="@+id/img_add"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/note_add_img" />

        <ImageView
            android:id="@+id/img_size"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/note_size" />

        <ImageView
            android:id="@+id/img_color"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/note_color" />



    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

4.待办事项

private void loadData() {
        executor.execute(() -> {
            List<AgentMattersBean> unfinishedData = agentMattersDao.getAllByUserPhoneAndStatus(phone, "未完成");
            List<AgentMattersBean> finishedData = agentMattersDao.getAllByUserPhoneAndStatus(phone, "已完成");

            int unfinishedCount = agentMattersDao.getCountByUserPhoneAndStatus(phone, "未完成");
            int finishedCount = agentMattersDao.getCountByUserPhoneAndStatus(phone, "已完成");

            // 对获取的数据进行倒序
            Collections.reverse(unfinishedData);
            Collections.reverse(finishedData);

            getActivity().runOnUiThread(() -> updateUI(unfinishedData, finishedData, unfinishedCount, finishedCount));
        });
    }

    private void updateUI(List<AgentMattersBean> unfinishedData, List<AgentMattersBean> finishedData, int unfinishedCount, int finishedCount) {
        // 更新未完成列表视图
        if (unfinishedData.isEmpty()) {
            llUnfinished.setVisibility(View.GONE);
            lvUnfinished.setAdapter(null); // 清空 ListView
        } else {
            llUnfinished.setVisibility(View.VISIBLE);
            AgentMattersAdapter unfinishedAdapter = new AgentMattersAdapter(getActivity(), unfinishedData, this);
            lvUnfinished.setAdapter(unfinishedAdapter);
            unfinishedAdapter.notifyDataSetChanged(); // 确保 Adapter 刷新
        }

        // 更新已完成列表视图
        if (finishedData.isEmpty()) {
            llFinish.setVisibility(View.GONE);
            lvFinish.setAdapter(null); // 清空 ListView
        } else {
            llFinish.setVisibility(View.VISIBLE);
            AgentMattersAdapter finishedAdapter = new AgentMattersAdapter(getActivity(), finishedData, this);
            lvFinish.setAdapter(finishedAdapter);
            finishedAdapter.notifyDataSetChanged(); // 确保 Adapter 刷新
        }

        // 更新计数和标题
        tv1.setText(unfinishedCount > 0 ? "未完成" : "");
        tvUnfinished.setText(unfinishedCount > 0 ? unfinishedCount + "项" : "");
        tv2.setText(finishedCount > 0 ? "已完成" : "");
        tvFinish.setText(finishedCount > 0 ? finishedCount + "项" : "");

        // 显示或隐藏信息视图
        tvShow.setVisibility(unfinishedCount == 0 && finishedCount == 0 ? View.VISIBLE : View.GONE);
    }

这个方法的功能点总结如下:

1. **加载数据**:
   - 在后台线程中,通过 `agentMattersDao` 从数据库中获取未完成和已完成的事务数据。
   - 同时获取未完成和已完成事务的数量。
   - 对获取的数据进行倒序排列。

2. **更新用户界面**:
   - 在主线程中执行 UI 更新操作。
   - **未完成事务**:
     - 如果未完成事务数据为空,隐藏相关视图并清空 `ListView`。
     - 如果有未完成事务,显示相关视图,并使用 `AgentMattersAdapter` 更新 `ListView`,确保适配器刷新。
   - **已完成事务**:
     - 如果已完成事务数据为空,隐藏相关视图并清空 `ListView`。
     - 如果有已完成事务,显示相关视图,并使用 `AgentMattersAdapter` 更新 `ListView`,确保适配器刷新。
   - 更新计数和标题:
     - 设置未完成和已完成事务的数量及其显示的文本。
   - 显示或隐藏提示信息视图:
     - 当未完成和已完成事务数量都为 0 时,显示提示信息视图;否则隐藏。

5.心情日记页、添加修改页

 private void show() {
        // 创建线程池并执行注册操作
        executor.execute(() -> {
            List<DiaryBean> diaryBeans = diaryDao.getAllDiariesByUserPhone(phone);
            getActivity().runOnUiThread(() -> {
                if (diaryBeans != null && !diaryBeans.isEmpty()) {
                    // 排序 diaryBeans 列表
                    Collections.sort(diaryBeans, new Comparator<DiaryBean>() {
                        @Override
                        public int compare(DiaryBean d1, DiaryBean d2) {
                            try {
                                SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
                                Date date1 = inputFormat.parse(d1.getTime());
                                Date date2 = inputFormat.parse(d2.getTime());
                                if (date1 != null && date2 != null) {
                                    // 反转 compareTo 的结果以实现降序排序
                                    return date2.compareTo(date1);
                                }
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            return 0;
                        }
                    });
                    // 隐藏 TextView,显示 ListView
                    tvShow.setVisibility(View.GONE);
                    lv.setVisibility(View.VISIBLE);

                    // 创建适配器并设置到 ListView
                    DiaryAdapter adapter = new DiaryAdapter(getActivity(), diaryBeans);
                    lv.setAdapter(adapter);
                } else {
                    // 显示 TextView,隐藏 ListView
                    tvShow.setVisibility(View.VISIBLE);
                    lv.setVisibility(View.GONE);
                }
            });
        });
    }

    private void add() {
        imgAdd.setOnClickListener(v -> {
            if (TextUtils.isEmpty(phone)) {
                Toast.makeText(getActivity(), "请先登录在进行操作!", Toast.LENGTH_SHORT).show();
            } else {
                // 获取当前日期
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);

                // 创建 DatePickerDialog
                DatePickerDialog datePickerDialog = new DatePickerDialog(
                        getActivity(),
                        (view, selectedYear, selectedMonth, selectedDay) -> {
                            // 格式化选择的日期
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
                            Calendar selectedDate = Calendar.getInstance();
                            selectedDate.set(selectedYear, selectedMonth, selectedDay);
                            String formattedDate = sdf.format(selectedDate.getTime());

                            // 创建 Intent 跳转到 DiaryActivity
                            Intent intent = new Intent(getActivity(), DiaryActivity.class);
                            intent.putExtra("date", formattedDate);
                            startActivity(intent);
                        },
                        year,
                        month,
                        day
                );
                // 显示 DatePickerDialog
                datePickerDialog.show();
            }
        });
    }

这个代码的功能点总结如下:

1. **显示日记数据 (`show` 方法)**:
   - 在后台线程中获取用户的所有日记数据。
   - 如果日记数据不为空:
     - 按日期降序对日记数据进行排序。
     - 隐藏提示 `TextView` 并显示 `ListView`。
     - 创建并设置适配器以显示日记数据。
   - 如果日记数据为空:
     - 显示提示 `TextView` 并隐藏 `ListView`。

2. **添加新日记 (`add` 方法)**:
   - 为添加按钮设置点击事件监听器。
   - 如果用户未登录,显示提示信息。
   - 如果用户已登录:
     - 显示 `DatePickerDialog` 让用户选择日期。
     - 格式化选择的日期并创建 `Intent` 跳转到 `DiaryActivity`,传递所选日期。

6.我的页面、修改密码、用户名

 private void modify() {
        // 获取传递过来的信息类型(密码或用户名)
        String information = getIntent().getStringExtra("information");

        if (information != null) {
            // 根据信息类型设置界面
            if (information.equals("password")) {
                // 如果信息类型是"password",则设置密码相关的提示文本
                etNew.setHint("请输入新密码(6-16位)");
                etRnew.setHint("请再次输入新密码(6-16位)");
                tvTitle.setText("修改用户密码");

                // 设置修改按钮的点击事件
                btnModify.setOnClickListener(v -> {
                    // 获取输入的手机号、新密码和确认密码
                    String etPhone = this.etPhone.getText().toString();
                    String newInformation = etNew.getText().toString();
                    String rnewInformation = etRnew.getText().toString();

                    // 检查手机号是否为空
                    if (etPhone.isEmpty()) {
                        Toast.makeText(this, "手机号不能为空!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 检查输入的手机号是否与当前用户手机号一致
                    if (!etPhone.equals(phone)) {
                        Toast.makeText(this, "请检查手机号是否一致!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 检查新密码长度是否符合要求
                    if (newInformation.length() < 6 || newInformation.length() > 16) {
                        Toast.makeText(this, "密码长度必须在 6 到 16 位之间!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 检查新密码和确认密码是否一致
                    if (!newInformation.equals(rnewInformation)) {
                        Toast.makeText(this, "新密码与确认密码不一致!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 使用线程池异步执行密码更新操作
                    executorService.execute(() -> {
                        boolean success = userDao.updateUserPasswordByPhone(phone, newInformation);

                        runOnUiThread(() -> {
                            if (success) {
                                // 清除SharedPreferences中的用户数据
                                SharedPreferences sharedPreferences = getSharedPreferences("User", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.clear();
                                editor.apply();

                                // 显示修改成功消息并跳转到主界面
                                Toast.makeText(this, "修改成功,请重新登录!", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ModifyInformationActivity.this, MainActivity.class);
                                intent.putExtra("state", "mine");
                                startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(this, "修改失败,请联系管理员!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    });
                });

            } else if (information.equals("userName")) {
                // 如果信息类型是"userName",则设置用户名相关的提示文本
                etNew.setHint("请输入新用户名");
                etRnew.setHint("请再次输入新用户名");
                tvTitle.setText("修改用户名");

                // 设置修改按钮的点击事件
                btnModify.setOnClickListener(v -> {
                    // 获取输入的手机号、新用户名和确认用户名
                    String etPhone = this.etPhone.getText().toString();
                    String newInformation = etNew.getText().toString();
                    String rnewInformation = etRnew.getText().toString();

                    // 检查手机号是否为空
                    if (etPhone.isEmpty()) {
                        Toast.makeText(this, "手机号不能为空!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 检查输入的手机号是否与当前用户手机号一致
                    if (!etPhone.equals(phone)) {
                        Toast.makeText(this, "请检查手机号是否一致!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 检查新用户名和确认用户名是否一致
                    if (!newInformation.equals(rnewInformation)) {
                        Toast.makeText(this, "新用户名与确认用户名不一致!", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    // 使用线程池异步执行用户名更新操作
                    executorService.execute(() -> {
                        boolean success = userDao.updateUserNameByPhone(phone, newInformation);

                        runOnUiThread(() -> {
                            if (success) {
                                // 显示修改成功消息并跳转到主界面
                                Toast.makeText(this, "修改成功!", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ModifyInformationActivity.this, MainActivity.class);
                                intent.putExtra("state", "mine");
                                startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(this, "修改失败,请联系管理员!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    });
                });
            }
        }
    }

1. 修改密码:
   - 界面设置:
     - 如果传递的 `information` 为 `"password"`,则设置密码相关的提示文本和标题。
   - 点击事件处理:
     - 获取输入的手机号、新密码和确认密码。
     - 验证手机号是否为空且是否与当前用户手机号一致。
     - 验证新密码的长度是否在 6 到 16 位之间。
     - 验证新密码和确认密码是否一致。
   - 异步操作:
     - 使用线程池异步执行密码更新操作。
     - 更新成功后,清除 `SharedPreferences` 中的用户数据,显示成功消息,跳转到主界面并结束当前活动。
     - 更新失败时,显示错误消息。
2. 修改用户名:
   - 界面设置:
     - 如果传递的 `information` 为 `"userName"`,则设置用户名相关的提示文本和标题。
   - 点击事件处理:
     - 获取输入的手机号、新用户名和确认用户名。
     - 验证手机号是否为空且是否与当前用户手机号一致。
     - 验证新用户名和确认用户名是否一致。
   - 异步操作:
     - 使用线程池异步执行用户名更新操作。
     - 更新成功后,显示成功消息,跳转到主界面并结束当前活动。
     - 更新失败时,显示错误消息。
 

四、项目完整源码

👇👇👇👇👇快捷获取方式👇👇👇👇👇

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程乐学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值