直播系统源码,自动登录及记住密码实现

发布时间 2023-10-26 14:03:36作者: 云豹科技-苏凌霄

直播系统源码,自动登录及记住密码实现

分为两个activity,mainActivity是登录页面,homeActivity是登录成功页面。

HomeActivity.java代码

 


public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
  }
}
 

activity_home.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"
tools:context=".HomeActivity">
<TextView
    android:id="@+id/tv_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:textSize="26sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
 

 

MainActivity.java代码


private AppCompatEditText edit_account,  edit_password;
private CheckBox cb_remember, cb_autologin;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bindView();
    initView();
}
/**
 *用于绑定控件id的方法
 */
protected void bindView() {
    edit_account = findViewById(R.id.edit_account);
    edit_password = findViewById(R.id.edit_password);
    cb_remember = findViewById(R.id.cb_remember);
    cb_remember.setOnCheckedChangeListener(this);
    cb_autologin = findViewById(R.id.cb_autologin);
    cb_autologin.setOnCheckedChangeListener(this);
    Button btn_login = findViewById(R.id.btn_login);
    btn_login.setOnClickListener(this);
    // 获取SharedPreferences的实例
    sharedPreferences = this.getSharedPreferences("loginInfo", MODE_PRIVATE);
}
/**
 * 用于初始化界面
 */
protected void initView() {
// 获取sharedPreferences中remember对于的boolean值,true表示记住密码
    if (sharedPreferences.getBoolean("remember", false)) {
        cb_remember.setChecked(true);
        edit_account.setText(sharedPreferences.getString("account", ""));
        edit_password.setText(sharedPreferences.getString("password",""));
        autologin();
    }
}
// 登录按钮的逻辑
@Override
public void onClick(View view) {
    // 定义账号和密码的字符串
    String account, password;
    // 判断账号是否为空
    if (edit_account.getText() == null) {
        showToast("账号为空,请重新输入");
        return;
    }
    // 判断密码是否为空
    if (edit_password.getText() == null) {
        showToast("密码为空,请重新输入");
        return;
    }
    // 账号和密码都不为空,进行密码账号校验
    account = edit_account.getText().toString().trim();
    password = edit_password.getText().toString().trim();
    // 此处固定了账号和密码
    if (account.equals("admin") && password.equals("12345")) {
        if (cb_remember.isChecked()) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("account", account);
            editor.putString("password", password);
            editor.apply();
        }
        showToast("登录成功");
        Intent intent = new Intent(MainActivity.this, HomeActivity.class);// 跳转到主界面
        startActivity(intent);
//            finish();
    }
}

 以上就是直播系统源码,自动登录及记住密码实现, 更多内容欢迎关注之后的文章