Kotlin开发:如何将SharedPreferences存储的密码从设置Fragment传递到锁屏Fragment
功能实现与优化方案
你已经通过SharedPreferences完成了密码持久化存储,无需额外做跨页面参数传递,直接在锁屏页读取相同SP文件的密码即可完成校验,以下是完整实现说明与代码修正:
现有代码问题修正
你当前clear()方法存在逻辑错误:存储passwordStatus时是和密码同存在名为Password的SP文件中,但清除时你单独创建了名为passwordStatus的SP文件操作,会导致清除密码功能不生效,修改后的代码如下:
private fun clear() { val passKey = "Password" val sharedPreference = requireActivity().getSharedPreferences(passKey, Context.MODE_PRIVATE) sharedPreference.edit().clear().apply() }
锁屏页读取密码实现
直接在LockScreenFragment中读取同一SP文件的存储内容即可,修改后的LockScreenFragment代码如下:
class LockScreenFragment : Fragment() { // 定义SP文件名常量,和设置页保持一致 private val PASS_KEY = "Password" override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_lock_screen, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // 读取SP中存储的密码和密码状态 val sharedPref = requireActivity().getSharedPreferences(PASS_KEY, Context.MODE_PRIVATE) val savedPassword = sharedPref.getString("password", "") val isPasswordSet = sharedPref.getBoolean("passwordStatus", false) // 此处添加密码校验逻辑:获取用户在锁屏页输入的密码和savedPassword比对即可 // 示例逻辑参考: // val inputPassword = view.findViewById<EditText>(R.id.lock_password_input).text.toString() // if (inputPassword == savedPassword) { // // 校验通过,跳转至主页面 // } else { // // 提示密码错误 // } } }
附:原代码中文翻译(含提示文本)
密码存储核心代码
private fun createPassword() { val passKey = "Password" val sharedPreference = requireActivity().getSharedPreferences(passKey, Context.MODE_PRIVATE) val editor = sharedPreference.edit() editor.putString("password", mSetPasswordBinding.password.text.toString()) editor.putBoolean("passwordStatus", true) editor.apply() }
CreatePasswordFragment完整代码
class CreatePasswordFragment : DialogFragment() { lateinit var mSetPasswordBinding: FragmentCreatePasswordBinding override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // 加载当前Fragment的布局 mSetPasswordBinding = DataBindingUtil.inflate( layoutInflater, R.layout.fragment_create_password, container, false ) mSetPasswordBinding.submit.setOnClickListener { checkPassword() } mSetPasswordBinding.clear.setOnClickListener { clear() } val passKey = "Password" val sharedPreference = requireActivity().getSharedPreferences(passKey, Context.MODE_PRIVATE) sharedPreference.getString("password", "defaultName") mSetPasswordBinding.submit.text = sharedPreference.getString("password", "") if (sharedPreference.getBoolean("passwordStatus", false)) { mSetPasswordBinding.passwordStatus.text = "修改密码" } return mSetPasswordBinding.root } private fun checkPassword(): Boolean { val getEditTextValue = mSetPasswordBinding.password.text.toString() val getRepeatEditTextValue = mSetPasswordBinding.repeat.text.toString() if (getEditTextValue != getRepeatEditTextValue) { mSetPasswordBinding.repeatL.error = "两次输入的密码不一致" } else { mSetPasswordBinding.repeatL.error = null createPassword() Toast.makeText(activity, "密码设置成功", Toast.LENGTH_SHORT).show() findNavController().popBackStack() return true } return true } private fun createPassword() { val passKey = "Password" val sharedPreference = requireActivity().getSharedPreferences(passKey, Context.MODE_PRIVATE) val editor = sharedPreference.edit() editor.putString("password", mSetPasswordBinding.password.text.toString()) editor.putBoolean("passwordStatus", true) editor.apply() } private fun clear() { val passKey = "Password" val sharedPreference = requireActivity().getSharedPreferences(passKey, Context.MODE_PRIVATE) sharedPreference.edit().clear().apply() } }
密码设置页布局代码(中文提示版本)
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/for_pop_up_square" android:orientation="vertical" android:id="@+id/t1" tools:context=".ui.createPassword.CreatePasswordFragment"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/passwordStatus" android:layout_marginLeft="2dp" android:layout_marginTop="5dp" android:layout_marginRight="2dp" android:gravity="center" android:text="登录密码" android:textColor="@color/black" android:textSize="20sp" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:layout_marginTop="20sp" android:layout_marginRight="20sp" android:orientation="vertical"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/cardNumberLt" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="登录密码"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:layout_marginTop="20sp" android:layout_marginRight="20sp" android:orientation="vertical"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/repeatL" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="确认密码"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/repeat" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:layout_marginTop="20sp" android:layout_marginRight="20sp" android:layout_marginBottom="30sp" android:orientation="vertical"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/rememberl" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码提示"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/remember" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> </LinearLayout> <Button android:id="@+id/submit" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:text="确认" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/clear" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:text="清除密码" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
内容的提问来源于stack exchange,提问作者AmirHossein
相关产品推荐
相关产品推荐

