您的位置:首页 > 其它

【数据存储全方案,详解 持久化技术】实现记住密码功能

2018-02-27 16:13 726 查看


login.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">

<TableRow>

<TextView
android:layout_height="wrap_content"
android:text="Account:" />

<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>

<TableRow>

<TextView
android:layout_height="wrap_content"
android:text="Password:" />

<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>

<TableRow>

<CheckBox
android:id="@+id/remember_pass"
android:layout_height="wrap_content" />

<TextView
android:layout_height="wr
4000
ap_content"
android:text="Remember password" />
</TableRow>

<TableRow>

<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>
</TableLayout>


LoginActivity

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
private CheckBox rememberPass;

private SharedPreferences preferences;
private SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
accountEdit = (EditText) findViewById(R.id.account);
passwordEdit = (EditText) findViewById(R.id.password);
rememberPass = (CheckBox) findViewById(R.id.remember_pass);
login = (Button) findViewById(R.id.login);

preferences = PreferenceManager.getDefaultSharedPreferences(this);

boolean isRemember = preferences.getBoolean("remember_password",false);

if(isRemember){
//将账号和密码都设置到文本框
String account = preferences.getString("account","");
String password = preferences.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account = accountEdit.getText().toString();
String password = passwordEdit.getText().toString();

if (account.equals("admin") && password.equals("123456")) {
editor = preferences.edit();
if(rememberPass.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("account",account);
editor.putString("password",password);
}else{
editor.clear();
}
editor.commit();

Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "accountorpassword is invalid", Toast.LENGTH_SHORT).show();
}
}
});
}
}


对代码的解释

首先在 onCreate()方法中获取到了 SharedPreferences对象,然后调用它 的 getBoolean()方法去获取 remember_password 这个键对应的值,一开始当然不存在对应的 值了,所以会使用默认值 false,这样就什么都不会发生

接着在登录成功之后,会调用 CheckBox的 isChecked()方法来检查复选框是否被选中,如果被选中了表示用户想要记住密 码,这时将 remember_password设置为 true,然后把 account和 password对应的值都存入到 SharedPreferences 文件当中并提交

如果没有被选中,就简单地调用一下 clear()方法,将 SharedPreferences文件中的数据全部清除掉。 当用户选中了记住密码复选框,并成功登录一次之后,remember_password键对应的值 就是 true了,这个时候如果再重新启动登录界面,就会从 SharedPreferences文件中将保存的 账号和密码都读取出来,并填充到文本输入框中,然后把记住密码复选框选中,这样就完成 记住密码的功能了

不过需要注意,这里实现的记住密码功能仍然还只是个简单的示例,并不能在实际的项 目中直接使用。因为将密码以明文的形式存储在 SharedPreferences文件中是非常不安全的, 很容易就会被别人盗取,因此在正式的项目里还需要结合一定的加密算法来对密码进行保护 才行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: