【发布时间】:2020-07-10 09:23:35
【问题描述】:
通常,我使用代码 A 或代码 B 来读取或写入 SharedPreferences。
目前,我将我的项目更新为在 Kotlin 中使用 "androidx.preference:preference-ktx:1.1.1"。
当我在 Kotlin 中使用 "androidx.preference:preference-ktx:1.1.1" 时,有没有更好的方法来读写 SharedPreferences ?
代码 A
SharedPreferences prfs = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE);
String Astatus = prfs.getString("Authentication_Status", "");
代码 B
SharedPreferences preferences = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Authentication_Id",userid.getText().toString());
editor.putString("Authentication_Password",password.getText().toString());
editor.putString("Authentication_Status","true");
editor.apply();
【问题讨论】:
-
只需将上面的代码转换为 kotlin。