简体   繁体   English

EditText 不由 SharedPreferences 保存

[英]EditText isn't saved by SharedPreferences

I'm supposed to save three EditTexts' strings using SharedPreferences so I can use them in a TextView when user re-enter the app.我应该使用SharedPreferences保存三个 EditTexts 的字符串,这样当用户重新进入应用程序时,我可以在 TextView 中使用它们。 This is what I have tried:这是我尝试过的:

public class MainActivity extends AppCompatActivity {

    private EditText PrivName;
    private EditText LastName;
    private EditText LastMov;
    SharedPreferences sp;
    private Button SaveBtn;
    private TextView Text;
    String Priv, Lst, Mov;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PrivName = (EditText)findViewById(R.id.PrivName);
        LastName = (EditText)findViewById(R.id.LastName);
        LastMov = (EditText)findViewById(R.id.LastMov);
        SaveBtn = (Button)findViewById(R.id.SaveBtn);
        Text = (TextView) findViewById(R.id.Message);

        sp = getSharedPreferences("Movies", Context.MODE_PRIVATE);

        SaveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Priv = PrivName.getText().toString();
                Lst = LastName.getText().toString();
                Mov = LastMov.getText().toString();

                SharedPreferences.Editor editor = sp.edit();

                editor.putString("Name", Priv);
                editor.putString("Name2", Lst);
                editor.putString("Movie", Mov);
                editor.commit();
            }
        });
        Text.setText("We hope that you  " + Priv + " "  +Lst + " enjoyed the movie: " + Mov);
    }
}

Obviously the problem is that when you click the button the TextView changes to what you want but when you re-enter the app everything resets instead of staying the same显然问题在于,当您单击按钮时,TextView 会更改为您想要的,但是当您重新进入应用程序时,一切都会重置而不是保持不变

You need to also read from the SharedPrefs, so you can actually fill the EditText.您还需要从 SharedPrefs 中读取数据,以便实际填充 EditText。 Try something like that:尝试这样的事情:

Add this in your onCreate method在你的 onCreate 方法中添加这个

sp = getSharedPreferences("Movies", Context.MODE_PRIVATE);
PrivName.setText(sp.getString("Name", "")); //The empty string after the comma is a default value incase you don't a stored value for that key
LastName.setText(sp.getString("Name2", ""));
LastMov.setText(sp.getString("Movie", ""));

Invoke the following method at the end of your onCreate method.onCreate方法的末尾调用以下方法。

private void showSavedValue()
{
  // Retaining the values from shared preferences
  String savedPrivName = sp.getString("Name",""); // If no value was stored previously it will return an empty string
  String savedLastName = sp.getString("Name2", "");
  String savedLastMovie = sp.getString("Movie","");

  //Write your code here to use these values where you want to use them


}

Note: Try to follow the recommended naming convention while writing Java.注意:在编写 Java 时尽量遵循推荐的命名约定。 A detailed guide on Java naming conventions can be found here可以在此处找到有关 Java 命名约定的详细指南

Look at your source code properly what you are doing..正确查看您的源代码您在做什么..

editor.putString("Name", Priv);
editor.putString("Name2", Lst);
editor.putString("Movie", Mov);
editor.commit();

Here you are saving some parameter.. Where "Name" is a id and, you are saving Priv strings to that id.. Same is happening to Name2 and Movie.在这里,您正在保存一些参数。其中"Name"是一个 id,并且您正在将Priv字符串保存到该 id 中。Name2 和电影也是如此。

Priv = PrivName.getText().toString();
Lst = LastName.getText().toString();
Mov = LastMov.getText().toString();

You are getting texts from Edittexts and saving them to Priv,Lst,Mov .您正在从 Edittexts 获取文本并将它们保存到Priv,Lst,Mov But, they aren't working as sharedPreferences.但是,它们不能作为 sharedPreferences 工作。 That means when you restart the application these values will be gone...这意味着当您重新启动应用程序时,这些值将消失......

Text.setText("We hope that you  " + Priv + " "  +Lst + " enjoyed the movie: " + Mov);

In the above source code you are showing those values which will be gone if you restart.在上面的源代码中,您显示了重新启动后将消失的那些值。 And, you wrote that code to onCreate method.而且,您将该代码写入 onCreate 方法。 So, when you are starting application these values are showing but, which has no value( null ).因此,当您启动应用程序时,这些值会显示,但是没有值( null )。

Here what you should try.. @Niaz Ahmed 's answer is correct but, his explanation is very poor that's why I am posting another answer.您应该在这里尝试.. @Niaz Ahmed答案是正确的,但是他的解释很差,这就是我发布另一个答案的原因。

String savedPrivName = sp.getString("Name",""); // If no value was stored previously it will return an empty string


String savedLastName = sp.getString("Name2", "");
  String savedLastMovie = sp.getString("Movie","");

Text.setText("We hope that you  " + savedPrivName + " "  +savedLastName + " enjoyed the movie: " + savedLastMovie);

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

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