Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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
Java 由用户永久存储字符串?_Java_Android_Sharedpreferences_Settext - Fatal编程技术网

Java 由用户永久存储字符串?

Java 由用户永久存储字符串?,java,android,sharedpreferences,settext,Java,Android,Sharedpreferences,Settext,在我的主要活动中,我正在查看一个变量,如果该变量包含一个字符串(“示例字符串”),那么它将进入主屏幕。如果变量不包含任何内容(“”),它会将它们重定向到一个页面,在该页面上,它们可以通过editText输入值,然后永久存储该值。因此,下次他们打开应用程序时,它将具有此永久字符串(直到应用程序被删除),因此它将直接进入主屏幕。 从研究中我了解到我可能不得不使用共享偏好。我已经试过了,我觉得我做的不对。 请有人用一个代码示例来说明我发布的代码需要做些什么 MainActivity.class //t

在我的主要活动中,我正在查看一个变量,如果该变量包含一个字符串(“示例字符串”),那么它将进入主屏幕。如果变量不包含任何内容(“”),它会将它们重定向到一个页面,在该页面上,它们可以通过
editText
输入值,然后永久存储该值。因此,下次他们打开应用程序时,它将具有此永久字符串(直到应用程序被删除),因此它将直接进入主屏幕。 从研究中我了解到我可能不得不使用共享偏好。我已经试过了,我觉得我做的不对。 请有人用一个代码示例来说明我发布的代码需要做些什么

MainActivity.class

//this class uses the string, to see if its blank or contains a string

public class MainActivity extends Activity {


    public static final String Verified = ""; //Originally comes blank

         private EditText NumberET; //editText for user to enter a string


//the string verified is used in the main activity to determine which xml file to open.
// this is the class used to enter the string and permanently store it


public void onCreate(Bundle savedInstanceState)

 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verified);

Button VerifyCompleteButton = (Button) findViewById(R.id.VerifyCompleteButton);

VerifyCompleteButton.setOnClickListener(new View.OnClickListener()

{
   public void onClick(View view) 
   {    

     String Verified;
         String Number;


     Bundle bundle = getIntent().getExtras();

     Verified = bundle.getString("Verified");
     NumberString = bundle.getString("NumberString")

    Verified = NumberString.toString(); //set String Verified permenantly



   } 



   });
已验证。类

//this class uses the string, to see if its blank or contains a string

public class MainActivity extends Activity {


    public static final String Verified = ""; //Originally comes blank

         private EditText NumberET; //editText for user to enter a string


//the string verified is used in the main activity to determine which xml file to open.
// this is the class used to enter the string and permanently store it


public void onCreate(Bundle savedInstanceState)

 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verified);

Button VerifyCompleteButton = (Button) findViewById(R.id.VerifyCompleteButton);

VerifyCompleteButton.setOnClickListener(new View.OnClickListener()

{
   public void onClick(View view) 
   {    

     String Verified;
         String Number;


     Bundle bundle = getIntent().getExtras();

     Verified = bundle.getString("Verified");
     NumberString = bundle.getString("NumberString")

    Verified = NumberString.toString(); //set String Verified permenantly



   } 



   });

可以在上找到使用SharedReference的好例子

保存数据:

SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("KEY", "VALUE");
editor.commit();
获取数据:

SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE);
String savedValue = context.getString("KEY", "");
注意事项:

  • 确保您的[密钥]在保存和获取时相同。共享首选项基本上是一个hashmap/字典
  • 获取时,如果尚未找到或设置设置,请使用第二个参数设置默认返回值

    • 您可以这样使用,与上面的答案非常相似 用于保存字符串

      SharedPreferences sharedPref = getSharedPreferences("App_Name_Prefs",
                      Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPref.edit();
      editor.putString("Verified",  Verified);
      editor.commit();
      
      用于获取字符串

      SharedPreferences shared = getSharedPreferences("App_Name_Prefs",
                      Context.MODE_PRIVATE);
      String keyReturn = shared.getString(key, "");
      Log.d("return value" , "Verified is " + verified);
      

      你能详细说明一下钥匙是什么吗?还有getActivity()?我在getActivity()和getString上都出现了错误(由于getResource),我从一个片段复制了代码,在这种情况下需要getActivity。我编辑了我的答案以使用上下文。需要上下文来获取前缀和资源/字符串。因此,根据您运行此代码的位置,您需要或不需要上下文。键是一个唯一的字符串,您必须使用它来获取和设置您的首选项。例如,您可以使用“用户验证”。通常,这些键作为静态最终字符串存储在string.xml或类中。