版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39622065/article/details/83023613
本文记录了在AS环境下实现SharedPreference存储记住密码的功能。
首先实现布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="Accont:"/>
<EditText
android:id="@+id/account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="18sp"
android:text="PassWord:"/>
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:inputType="textPassword"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/rember_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Remember Password"/>
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_height="60dp"
android:layout_width="match_parent"
android:text="Login"
/>
</LinearLayout>
效果图:
之后进行代码逻辑编写:
package com.example.qing.secondd;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Main3Activity extends AppCompatActivity {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit,passwordEdit;
private Button login;
private CheckBox remberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
pref=PreferenceManager.getDefaultSharedPreferences(this);
accountEdit=(EditText)findViewById(R.id.account);
passwordEdit=(EditText)findViewById(R.id.password);
remberPass=(CheckBox) findViewById(R.id.rember_pass);
login=(Button)findViewById(R.id.login);
boolean isRemember=pref.getBoolean("remember_password",false);
if(isRemember){
String account=pref.getString("account","");
String password=pref.getString("password","");
accountEdit.setText(account);
passwordEdit.setText(password);
remberPass.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("123")){
editor=pref.edit();
if(remberPass.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("account",account);
editor.putString("password",password);
}
else{
editor.clear();
}
editor.apply();
Toast.makeText(Main3Activity.this, "Login Success!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Main3Activity.this,"wrong!",Toast.LENGTH_LONG).show();
}
}
});
}
}
本文是一个简单实现登陆的模板,当用户名:admin,密码:123,密码正确,显示登陆成功。
在实际项目中密码的存储还是需要进行加密存储。