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;
}
}

原文地址:https://www.cnblogs.com/Yforever/p/14912912.html