Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android应用程序中的保存设置_Android_Settings - Fatal编程技术网

android应用程序中的保存设置

android应用程序中的保存设置,android,settings,Android,Settings,我有一个显示一些项目和按钮的应用程序。用户可以隐藏这些按钮或项目中的任何一个。在设置中,用户可以删除按钮1: case R.id.action_delete: button1.setVisibility(View.GONE); return false; 它工作正常,但问题是当用户重新启动应用程序时,按钮1会再次出现。GoogleSharedReferences Android了解更多信息。设置设置后,您需要保存设置,然后在onCreate中读取并应用它们。如何在

我有一个显示一些项目和按钮的应用程序。用户可以隐藏这些按钮或项目中的任何一个。在设置中,用户可以删除按钮1:

case R.id.action_delete:
    button1.setVisibility(View.GONE);       
    return false;

它工作正常,但问题是当用户重新启动应用程序时,按钮1会再次出现。

Google
SharedReferences Android
了解更多信息。设置设置后,您需要保存设置,然后在
onCreate
中读取并应用它们。如何在共享首选项中保存设置?请为我编写设置,并在onCreate中编写代码。抱歉,我是android应用程序的新手,我需要检查上面的答案,它应该是这样工作的,如果它工作,请将其标记为答案:)您是否添加了
公共静态最终字符串PREFS_NAME=“PREFS”???把你的代码贴在这里,兄弟,我100%爱你的作品,但我想问你一件事。我很难理解发生了什么事。如果我想添加另一个save stat,比如如果我有另一个按钮调用button2,我是否应该再次编写代码,但对于button2,如果我有另一个then按钮,比如img,或者我还使用editor.putString(按钮”,“按钮”);,我很高兴它成功了:)如果您想添加更多,例如如果您想让按钮2也不可见,那么您的代码将如下:
case R.id.action\u delete:button1.setVisibility(View.go);button2.setVisibility(View.go);SharedPreferences preferences=getSharedPreferences(PREFS\u NAME,0);SharedReferences.Editor Editor=preferences.edit();Editor.putString(“按钮”、“按钮”);Editor.putString(“按钮2”、“隐藏”);Editor.commit();返回false;
您可以根据需要为它们命名,第一个是名称,第二个是给定的值
public static final String PREFS_NAME = "Prefs";


protected void onCreate(Bundle savedInstanceState) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("button", "").toString().equals("button")) {
button1.setVisibility(View.GONE);
        }
}



case R.id.action_delete:
     button1.setVisibility(View.GONE);
     SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
     SharedPreferences.Editor editor = preferences.edit();
     editor.putString("button", "button");
     editor.commit();  
     return false;