学习进度条

今日所花时间:一小时
今日代码量:100行
博客量:2篇
了解到的知识点: 深入学习androidStudio 完成学习记录APP开发
AndroidStudio代码的书写
功能实现代码:
RegisterActivity

package com.example.study0327;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.study0327.api.ApiService;
import com.example.study0327.api.RetrofitClient;
import com.example.study0327.modules.User;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    private EditText etStudentId, etUsername, etPhone, etClassName, etUnit, etPassword;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // 初始化视图
        etStudentId = findViewById(R.id.etStudentId);
        etUsername = findViewById(R.id.etUsername);
        etPhone = findViewById(R.id.etPhone);
        etClassName = findViewById(R.id.etClassName);
        etUnit = findViewById(R.id.etUnit);
        etPassword = findViewById(R.id.etPassword);
        btnRegister = findViewById(R.id.btnRegister);

        btnRegister.setOnClickListener(v -> {
            User user = new User (
                    etStudentId.getText().toString(),
                    etUsername.getText().toString(),
                    etPhone.getText().toString(),
                    etClassName.getText().toString(),
                    etUnit.getText().toString(),
                    etPassword.getText().toString()
            );

            ApiService apiService = RetrofitClient.getClient().create(ApiService.class);
            Call<String> call = apiService.registerUser(user);

            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.isSuccessful()) {
                        Toast.makeText(RegisterActivity.this, response.body(), Toast.LENGTH_SHORT).show();
                        // 注册成功后跳转到登录页面
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Toast.makeText(RegisterActivity.this, "注册失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        });
    }
}

LoginActivity

package com.example.study0327;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.study0327.api.ApiService;
import com.example.study0327.api.RetrofitClient;
import com.example.study0327.modules.User;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private EditText etStudentId, etPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etStudentId = findViewById(R.id.etStudentId);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(v -> {
            String studentId = etStudentId.getText().toString();
            String password = etPassword.getText().toString();

            ApiService apiService = RetrofitClient.getClient().create(ApiService.class);
            Call<String> call = apiService.loginUser(studentId, password);

            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.isSuccessful()) {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        // 登录成功后跳转到主页面
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Toast.makeText(LoginActivity.this, "登录错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        });
    }
}

AddRecordActivity

package com.example.study0327;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.study0327.api.ApiService;
import com.example.study0327.api.RetrofitClient;
import com.example.study0327.modules.ProgrammingRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddRecordActivity extends AppCompatActivity {
    private EditText etPspStage, etContent, etTimeSpent;
    private Button btnAddRecord;
    private int userId; // 从登录后获取

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_record);

        etPspStage = findViewById(R.id.etPspStage);
        etContent = findViewById(R.id.etContent);
        etTimeSpent = findViewById(R.id.etTimeSpent);
        btnAddRecord = findViewById(R.id.btnAddRecord);

        // 获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String currentDate = sdf.format(new Date());

        btnAddRecord.setOnClickListener(v -> {
            ProgrammingRecord record = new ProgrammingRecord();
            record.setUserId(userId);
            record.setRecordDate(currentDate);
            record.setPspStage(etPspStage.getText().toString());
            record.setContent(etContent.getText().toString());
            record.setTimeSpent(Integer.parseInt(etTimeSpent.getText().toString()));

            ApiService apiService = RetrofitClient.getClient().create(ApiService.class);
            Call<String> call = apiService.addProgrammingRecord(record);

            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    if (response.isSuccessful()) {
                        Toast.makeText(AddRecordActivity.this, response.body(), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    Toast.makeText(AddRecordActivity.this, "添加记录失败: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        });
    }
}

APP页面设计
activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        tools:context=".RegisterActivity">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户注册"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tvStudentId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvTitle" />

        <EditText
            android:id="@+id/etStudentId"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入学号"
            android:inputType="text"
            android:minHeight="48dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvStudentId" />

        <TextView
            android:id="@+id/tvUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etStudentId" />

        <EditText
            android:id="@+id/etUsername"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入姓名"
            android:inputType="textPersonName"
            android:minHeight="48dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvUsername" />

        <TextView
            android:id="@+id/tvPhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机号码:"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etUsername" />

        <EditText
            android:id="@+id/etPhone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入手机号码"
            android:inputType="phone"
            android:minHeight="48dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvPhone" />

        <TextView
            android:id="@+id/tvClassName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="班级:"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etPhone" />

        <EditText
            android:id="@+id/etClassName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入班级"
            android:inputType="text"
            android:minHeight="48dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvClassName" />

        <TextView
            android:id="@+id/tvUnit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="单位:"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etClassName" />

        <EditText
            android:id="@+id/etUnit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入单位"
            android:inputType="text"
            android:minHeight="48dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvUnit" />

        <TextView
            android:id="@+id/tvPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etUnit" />

        <EditText
            android:id="@+id/etPassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="输入密码"
            android:inputType="textPassword"
            android:minHeight="48dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvPassword" />

        <Button
            android:id="@+id/btnRegister"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="注册"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etPassword" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

activity_login.xml

<?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:padding="16dp"
    tools:context=".LoginActivity">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户登录"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvStudentId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学号:"
        android:layout_marginTop="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvTitle" />

    <EditText
        android:id="@+id/etStudentId"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:hint="输入学号"
        android:inputType="text"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvStudentId" />

    <TextView
        android:id="@+id/tvPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etStudentId" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:hint="输入密码"
        android:inputType="textPassword"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvPassword" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:text="登录"
        android:layout_marginTop="24dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etPassword" />

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:text="注册"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btnLogin" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_add_record.xml

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".AddRecordActivity">

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="添加编程记录"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/tvPspStage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PSP阶段:"
    android:layout_marginTop="24dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvTitle" />

    <EditText
        android:id="@+id/etPspStage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="输入PSP阶段"
        android:inputType="text"
        android:minHeight="48dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvPspStage" />

<TextView
    android:id="@+id/tvContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="内容:"
    android:layout_marginTop="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/etPspStage" />

<EditText
    android:id="@+id/etContent"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="输入内容"
    android:inputType="textMultiLine"
    android:minLines="3"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvContent" />

<TextView
    android:id="@+id/tvTimeSpent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="耗时(分钟):"
    android:layout_marginTop="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/etContent" />

    <EditText
        android:id="@+id/etTimeSpent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="输入耗时"
        android:inputType="number"
        android:minHeight="48dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvTimeSpent" />

<Button
    android:id="@+id/btnAddRecord"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="添加记录"
    android:layout_marginTop="24dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/etTimeSpent" />



</androidx.constraintlayout.widget.ConstraintLayout>
posted @ 2025-03-27 20:02  haoyinuo  阅读(45)  评论(0)    收藏  举报