zoukankan      html  css  js  c++  java
  • STD二手图书交流平台团队博客-登陆问题的解决

    解决登录问题

    遇到问题:登录时输入密码为明文

    在布局文件里面放入文本控件

    用于提示用户输入密码

    还有编辑框与选择框

    并增加可勾选选项显示密码

    完成后部分代码如下

    package com.example.secondhand;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.pm.PackageManager;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.text.InputType;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.TextView;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;

    //public class LoginActivity extends AppCompatActivity {
    public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView backArrow;
    private TextView tvTitle;
    private EditText etUserName;
    private EditText etPassword;
    private Button btnLogin;

    private Boolean bPwdSwitch = false;
    private EditText etPwd;

    private EditText etAccount;
    private CheckBox cbRememberPwd;

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

    btnLogin = findViewById(R.id.btnLogin);
    etUserName = findViewById(R.id.etUserName) ;backArrow = findViewById(R.id.backArrow);
    etPassword = findViewById(R.id.etPassword) ;
    backArrow.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    finish();
    }
    });
    tvTitle = findViewById(R.id.tvTitle);

    final ImageView ivPwdSwitch = findViewById(R.id.iv_pwd_switch);
    etPwd = findViewById(R.id.etPassword);

    ivPwdSwitch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    bPwdSwitch = !bPwdSwitch;
    if(bPwdSwitch){
    ivPwdSwitch.setImageResource(
    R.drawable.ic_visibility_black_24dp
    );
    etPwd.setInputType(
    InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
    );
    }else{
    ivPwdSwitch.setImageResource(
    R.drawable.ic_visibility_off_black_24dp
    );
    etPwd.setInputType(
    InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT
    );
    etPwd.setTypeface(Typeface.DEFAULT);
    }
    }
    });

    findViewById(R.id.tvRegister).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
    }
    });
    btnLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    startLogin();
    // startActivity(new Intent(LoginActivity.this,HomeActivity.class));
    }
    });
    verifyStoragePermissions(this);


    etPwd=findViewById(R.id.etPassword);
    etAccount=findViewById(R.id.etUserName);
    cbRememberPwd=findViewById(R.id.cb_remember_pwd);
    Button btLogin=findViewById(R.id.btnLogin);
    btLogin.setOnClickListener(this);

    String spFileName=getResources().getString(R.string.shared_preferences_file_name);
    String accountKey=getResources().getString(R.string.login_account_name);
    String passwordKey=getResources().getString(R.string.login_password);
    String rbPasswordKey=getResources().getString(R.string.login_remember_password);

    SharedPreferences spFile=getSharedPreferences(spFileName,MODE_PRIVATE);
    String account=spFile.getString(accountKey,null);
    String password=spFile.getString(passwordKey,null);
    Boolean rbPassword=spFile.getBoolean(rbPasswordKey,false);

    if(account!=null&&!TextUtils.isEmpty(account))
    {
    etAccount.setText(account);
    }
    if(password!=null&&!TextUtils.isEmpty(password))
    {
    etPwd.setText(password);
    }
    cbRememberPwd.setChecked(rbPassword);

    }

    public void onClick(View view)
    {
    startLogin();
    String spFileName=getResources().getString(R.string.shared_preferences_file_name);
    String accountKey=getResources().getString(R.string.login_account_name);
    String passwordKey=getResources().getString(R.string.login_password);
    String rbPasswordKey=getResources().getString(R.string.login_remember_password);

    SharedPreferences spFile=getSharedPreferences(spFileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=spFile.edit();

    if(cbRememberPwd.isChecked())
    {
    String password=etPwd.getText().toString();
    String account =etAccount.getText().toString();

    editor.putString(accountKey,account);
    editor.putString(passwordKey,password);
    editor.putBoolean(rbPasswordKey,true);
    editor.apply();
    }
    else
    {
    editor.remove(accountKey);
    editor.remove(passwordKey);
    editor.remove(rbPasswordKey);
    editor.apply();
    }
    editor.commit();
    }

    private final int REQUEST_EXTERNAL_STORAGE = 1;
    private String[] PERMISSIONS_STORAGE = {
    "android.permission.READ_EXTERNAL_STORAGE",
    "android.permission.WRITE_EXTERNAL_STORAGE" };


    public void verifyStoragePermissions(Activity activity) {
    try {
    //检测是否有写的权限
    int permission = ActivityCompat.checkSelfPermission(activity,
    "android.permission.WRITE_EXTERNAL_STORAGE");
    if (permission != PackageManager.PERMISSION_GRANTED) {
    // 没有写的权限,去申请写的权限,会弹出对话框
    ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private void startLogin() {
    String[] editTextContent = getEditTextContent();
    String username = editTextContent[0];
    String password = editTextContent[1];
    if (username == null || "".equals(username) || password == null || "".equals(password)) {
    //账户或者密码为空
    UiUtils.toast("用户名或者密码不能为空");
    return;
    }
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);
    User newUser = UserDao.getInstance().findUser(user);
    if (newUser != null){
    // UiUtils.toast("登录成功");
    MyApplication.username = username;
    MyApplication.user = newUser;
    System.out.println("currentTimeMillis Login:" + System.currentTimeMillis());
    startActivity(new Intent(LoginActivity.this,HomeActivity.class));
    }else {
    UiUtils.toast("登录失败");
    }
    }

    private String[] arr = new String[2];
    public String[] getEditTextContent(){
    String username = etUserName.getText().toString().trim();
    String password = etPassword.getText().toString().trim();
    arr[0] = username;
    arr[1] = password;
    return arr;
    }
    }

  • 相关阅读:
    Year Outline stat Detail stat 1987--1996----1999 C:UsersATIDocuments00drmmr v2 tafdrmmr1987-20
    atitit 2010 2010 diary log events memorabilia v3 taf .docx No finish , wait to finish 1.6 yLu
    Atitit 标记语言ML(Markup Language) v4 目录 1. 标记语言ML Markup Language 1 1.1. 简介 1 2. 置标语言置标语言通常可以分为三类:标识性的
    Atitit 2001drmmr v1 t05.docx 1.1shoeho kh majyao n chfe ,bg n rjywel ycyi ,shwa leihaivvei yaopao
    Atitit nlp重要节点 v3 目录 1. 语法分析重点 节点余额365个 1 2. nlp词性表 2 2.1. 词语分类13类 2 2.2. 副词 约20个 3 2.3. 代词30个 3 2
    Atitit 提升语法级别4gl 4.5g 4.9g 5g 目录 1. 语言级别表 1 2. 4.9g实现细节 2 2.1. $dollor前导符 2 2.2. Static变量 2 2.3. S
    Atitit 工程师程序员技术级别对应表与主要特征 P1--p6 说明 类别 职称 对应技术标志 P5 高级工程师 工程师类 一般四五年 P6 资深开发 工程师类 78年经历 P7 P7
    Atitit 自然语言与人工语言的语法构建ast的异同点 目录 1. 语言节点gaishu。。 2 1.1. 节点、函数数量大约200个 2 1.2. 关键词节点 是 有 的 3 1.3. 标识符
    Atitit 编程语言的block概念 目录 1. 匿名block 1 1.1. 函数块 方法快 1 1.2. Sp udf块 1 2. 实现block的方式 1 2.1. 早期的语言大多是采用en
    Atitit 效率提升法细则 v3 t028.docx Atitit 提升效率细则 目录 1. 目标 2 1.1. 配置化增加扩展性 尽可能消除编译 方便增加 调整业务逻辑 2 1.2. 统一接口
  • 原文地址:https://www.cnblogs.com/jz-no-bug/p/14760247.html
Copyright © 2011-2022 走看看