简体   繁体   English

在 Android Studio 中编辑和保存共享首选项

[英]Edit and save shared preferences in Android Studio

I can access shared preferences in Android Studio from Device file explorer, but after editing the file, the changed values aren't saved.我可以从设备文件资源管理器访问 Android Studio 中的共享首选项,但在编辑文件后,更改的值未保存。 How can I save the changes?如何保存更改?

I can access shared preferences in Android Studio from Device file explorer, but after editing the file, the changed values aren't saved.我可以从设备文件资源管理器访问 Android Studio 中的共享首选项,但是在编辑文件后,更改的值不会保存。 How can I save the changes?如何保存更改?

You're not editing the files directly.您不是直接编辑文件。 When you open a file that's on the device, Android Studio is actually pulling a copy to your local machine.当您打开设备上的文件时,Android Studio 实际上正在将副本拉取到您的本地计算机。

For example, if I selected this random i_log file from my SDCard it ends up here on my local machine under Documents on Mac (shown in the top status bar in Android Studio).例如,如果我从我的 SDCard 中选择了这个随机i_log文件,它最终会出现在我本地机器上的 Mac 上的Documents下(显示在 Android Studio 的顶部状态栏中)。 在此处输入图像描述

If you want to save the changes back to the device, you need to "upload" the file back to the device.如果要将更改保存回设备,则需要将文件“上传”回设备。

  1. On the folder in the device explorer you want to move the file to, right-click.在设备资源管理器中要将文件移动到的文件夹上,右键单击。
  2. Select Upload.. Select上传..

在此处输入图像描述

  1. In the file chooser dialog, navigate to the local copy you edited.在文件选择器对话框中,导航到您编辑的本地副本。

在此处输入图像描述

AS will push that file back to the device. AS 会将该文件推送回设备。

to access to shared preferences file that's identified by the resource string use the following code:要访问由资源字符串标识的共享首选项文件,请使用以下代码:

Kotlin: Kotlin:

var sharedPref = activity?.getSharedPreferences(
    getString(R.string.preference_file_key), Context.MODE_PRIVATE)

Java: Java:

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
    getString(R.string.preference_file_key), Context.MODE_PRIVATE);  

it using the private mode so the file is accessible by only your app.它使用私有模式,因此只有您的应用程序可以访问该文件。

you can use the following code to write to sharedpref:您可以使用以下代码写入 sharedpref:

Kotlin: Kotlin:

   val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
   with (sharedPref.edit()) {
       putInt("number", num)
       apply()
   }

Java: Java:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("number", num);
editor.apply();  

you can put boolean,string and etc to your sharedpref file.您可以将 boolean、字符串等放入您的 sharedpref 文件中。

then if you want to get that value you can say:然后,如果您想获得该值,可以说:

   val num = sharedPref.getInt("number", defvalue = 0 )  

as you know you can getBoolean, getString and etc如您所知,您可以 getBoolean、getString 等

Refer documentation as well.另请参阅文档

Alternatively you can also use a tool like Facebook's flipper which, amongst other things, allows you to edit SharedPreferences live .或者,您也可以使用像 Facebook 的flipper这样的工具,除其他外,它允许您实时编辑 SharedPreferences

Note, you need to integrate it with your app.请注意,您需要将其与您的应用程序集成。 It's fairly straightforward though and can be done in such a manner, that release versions are not affected by it at all.虽然它相当简单,并且可以以这样一种方式完成,即发布版本完全不受它的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM