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

今天:写了查看订单和自动登录功能,完成了测试,基本完成了业务流程,代码量200行。

复制代码
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);
    }
}
复制代码

 

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