效果图:
当我们输入完账号密码后,点击登录
关闭掉程序,再次重新启动程序,可以看到,账号密码已经自动填上
学习之前,我建议还没有学过 SharedPreferences 数据库的同学可以来参考一下我写的博文,再进行学习,相信聪明的你们一定很快就能掌握的了O(∩_∩)O
MainActivity.java
public class MainActivity extends AppCompatActivity { private SharedPreferences pref; // 取值对象 private SharedPreferences.Editor editor; // 存值对象 private CheckBox checkBox; // 记住密码 private EditText edtName,edtWord; // 账号,密码 private Button button; // 登录 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); // 初始化控件 pref = PreferenceManager.getDefaultSharedPreferences(this); // 进入是否记录密码键的结果中查询,勾选的话会返回true,否则返回false boolean isRemember = pref.getBoolean("remember_password",false); if (isRemember){ // 如果用户上次登录点击了记住密码按钮 // 将账号和密码都设置到文本框中 String username = pref.getString("name",""); String password = pref.getString("word",""); edtName.setText(username); edtWord.setText(password); checkBox.setChecked(true); // 设置勾选 } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = edtName.getText().toString(); String password = edtWord.getText().toString(); // 如果账号是 admin 且 密码是123456,就认为登录成功 if (username.equals("1234") && password.equals("123")){ editor = pref.edit(); if (checkBox.isChecked()){ // 检查复选框是否被选中 editor.putBoolean("remember_password",true); editor.putString("name",username); editor.putString("word",password); }else{ editor.clear(); } editor.apply(); // 提交 Intent intent = new Intent(MainActivity.this,TwoActivity.class); startActivity(intent); finish(); }else{ Toast.makeText(MainActivity.this,"账号或密码错误,请重新登录",Toast.LENGTH_LONG).show(); } } }); } private void initView() { edtName = findViewById(R.id.edt_name); edtWord = findViewById(R.id.edt_word); button = findViewById(R.id.btn_deng); checkBox = findViewById(R.id.cb_checkBox); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/edt_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:hint="请输入账号"/> <EditText android:id="@+id/edt_word" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:hint="请输入密码"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_checkBox" 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="记住密码"/> </LinearLayout> <Button android:id="@+id/btn_deng" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:textSize="20dp" android:text="登录"/> </LinearLayout>
TwoActivity 和 它的布局太简单,我就不列举了
注意:
这里实现的记住密码功能是一个简单的示例,并不能在实际的项目中直接使用。因为将密码以铭文的形式存储在 SharedPreferences 文件中是非常不安全的,很容易就会被别人盗取,因此再正式的项目里还需要结合一定的加密算法来对密码进行保护才行。