第九周周三(冲刺第三天)

今天:写了登录注册的部分代码,代码量300行,预计明天完成

明天:写完登录注册功能,核对接口。

两个activity:

复制代码
package com.example.kydy;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.kydy.BEAN.IPV4;
import com.example.kydy.BEAN.LoginInfo;
import com.example.kydy.Utils.CallBack;
import com.example.kydy.Utils.OkHttpUtils;
import com.google.gson.Gson;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_usernameL;
    private EditText et_passwordL;
    private Button btn_login;
    private TextView tv_toRegister;
    private CheckBox cb_auto_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

        // 检查是否有保存的自动登录信息
        SharedPreferences sharedPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
        boolean isAutoLogin = sharedPreferences.getBoolean("autoLogin", false);
        if (isAutoLogin) {
            String savedUsername = sharedPreferences.getString("username", "");
            String savedPassword = sharedPreferences.getString("password", "");
            autoLogin(savedUsername, savedPassword);
        }
    }

    private void initView() {
        et_usernameL = findViewById(R.id.et_usernameL);
        et_passwordL = findViewById(R.id.et_passwordL);
        btn_login = findViewById(R.id.btn_login);
        tv_toRegister = findViewById(R.id.tv_toRegister);
        cb_auto_login = findViewById(R.id.cb_auto_login);

        btn_login.setOnClickListener(this);
        tv_toRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_login){
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setUsername(et_usernameL.getText().toString());
            loginInfo.setPassword(et_passwordL.getText().toString());

            Gson gson = new Gson();
            String data = gson.toJson(loginInfo);
            OkHttpUtils.getInstance().doPost("http://" + IPV4.ipv4 + "/login/test?useSSL=true", new CallBack() {
                @Override
                public void onSuccess(String result) {
                    if (result.equals("1")){ //登录成功

                        // 保存登录信息
                        if (cb_auto_login.isChecked()) {
                            SharedPreferences sharedPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putBoolean("autoLogin", true);
                            editor.putString("username", loginInfo.getUsername());
                            editor.putString("password", loginInfo.getPassword());
                            editor.apply();
                        }

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra("username", loginInfo.getUsername());
                        startActivity(intent);
                        finish();

                    } else if (result.equals("0")) {
                        // 登录失败
                        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                        builder.setMessage("登录失败,用户名或密码错误");
                        builder.setPositiveButton("确定", null);
                        AlertDialog alertDialog = builder.create();
                        alertDialog.show();
                    } else {
                        // 登录失败
                        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                        builder.setMessage("登录失败,请检查网络设置");
                        builder.setPositiveButton("确定", null);
                        AlertDialog alertDialog = builder.create();
                        alertDialog.show();
                    }
                }

                @Override
                public void onError(Exception e) {
                    e.printStackTrace();
                    // 登录失败
                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                    builder.setMessage("登录失败,请检查网络设置");
                    builder.setPositiveButton("确定", null);
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();
                }
            }, data);

        } else if (view.getId() == R.id.tv_toRegister) {
            //跳转注册
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    }

    private void autoLogin(String username, String password) {
        // 自动登录逻辑,与手动登录类似
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(username);
        loginInfo.setPassword(password);

        Gson gson = new Gson();
        String data = gson.toJson(loginInfo);
        OkHttpUtils.getInstance().doPost("http://" + IPV4.ipv4 + "/login/test?useSSL=true", new CallBack() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("1")) { // 登录成功
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("username", username);
                    startActivity(intent);
                    finish();
                } else {

                }
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }
        }, data);
    }
}
复制代码
复制代码
package com.example.kydy;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toolbar;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.kydy.BEAN.IPV4;
import com.example.kydy.BEAN.RegisterInfo;
import com.example.kydy.Utils.CallBack;
import com.example.kydy.Utils.OkHttpUtils;
import com.google.gson.Gson;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et_username;
    private EditText et_password;
    private EditText et_nickname;
    private EditText et_phone_number;
    private EditText et_avatar_url;
    private Button btn_subRegister;
    private androidx.appcompat.widget.Toolbar tb_toLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

    }

    private void initView() {
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        et_nickname = findViewById(R.id.et_nickname);
        et_phone_number = findViewById(R.id.et_phone_number);
        //et_avatar_url = findViewById(R.id.et_avatar_url);
        btn_subRegister = findViewById(R.id.btn_subRegister);
        tb_toLogin = findViewById(R.id.tb_toLogin);

        btn_subRegister.setOnClickListener(this);
        tb_toLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tb_toLogin){
            //关闭该activity
            finish();
        } else if (view.getId() == R.id.btn_subRegister) {
            RegisterInfo registerInfo = new RegisterInfo();
            registerInfo.setUsername(et_username.getText().toString());
            registerInfo.setPassword(et_password.getText().toString());
            registerInfo.setNickname(et_password.getText().toString());
            registerInfo.setPhone_number(et_phone_number.getText().toString());


            // 检查手机号是否合法
            if (!isValidPhoneNumber(registerInfo.getPhone_number())) {
                // 如果手机号不合法,弹出提示框
                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                builder.setMessage("请输入有效的手机号码");
                builder.setPositiveButton("确定", null);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            } else {
                // 创建一个 Gson 对象
                Gson gson = new Gson();

                String json = gson.toJson(registerInfo);
                OkHttpUtils.getInstance().doPost("http://" + IPV4.ipv4 + "/register/add?useSSL=true", new CallBack() {

                    @Override
                    public void onSuccess(String result) {
                        if (result.equals("success")) {//成功
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("注册成功!")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            // 在这里处理确定按钮点击事件
                                            finish();
                                        }
                                    });
                            AlertDialog dialog = builder.create();
                            dialog.show();

                        } else if (result.equals("already")) {//已存在
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("用户名已存在")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            // 在这里处理确定按钮点击事件
                                        }
                                    });
                            AlertDialog dialog = builder.create();
                            dialog.show();

                        } else {//异常,网络错误
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("网络错误,请检查网络设置")
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            // 在这里处理确定按钮点击事件
                                            finish();
                                        }
                                    });
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }

                    }

                    @Override
                    public void onError(Exception e) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                        builder.setMessage("网络错误,请检查网络设置")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // 在这里处理确定按钮点击事件
                                        finish();
                                    }
                                });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                }, json);

            }
        }

    }

    // 检查手机号是否合法的方法
    private boolean isValidPhoneNumber(String phoneNumber) {
        // 中国大陆手机号正则表达式
        String regex = "^1[3456789]\\d{9}$";
        return phoneNumber.matches(regex);
    }
}
复制代码

 

posted @   a_true  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2023-04-24 pta_虚函数的应用及收获
点击右上角即可分享
微信分享提示