Save value of EditText with SharedPreferences and without using a button (Android)

3

I would like to know if it is possible to store the information of an EditText without having to click on any type of button, that is, at the same time that the user writes can be saved, and be accessible when the application starts again . The idea would be that the first time the user starts the application, they could enter their data manually, and at that moment they will be saved, so that when they reload the application they would be available and visible to the user.

My application starts with this view, and what I want to do is save the data at the same time they are written.

The thing is that if I had a button it would be easy enough to save it, but I would like to do it without a button. My idea is to save it with sharedPreferences, although the DNI will also save it with sqLite. I know that EditText has these events:

 editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Fires right as the text is being changed (even supplies the range of text)

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // Fires right before text is changing
        }

        @Override
        public void afterTextChanged(Editable s) {
            // Fires right after the text has changed

        }
    });

but, how do I make it so that once I write the value I can save it? Thank you very much, best regards.

    
asked by Aico 07.04.2017 в 19:35
source

2 answers

3

You can get the content of EditText complete in afterTextChanged and then save it as:

    @Override
    public void afterTextChanged(Editable s) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
        // te da las preferencias
        prefs.edit().putString("editText",s.toString()).commit(); // guardarlos
    }
    
answered by 11.04.2017 в 22:40
1
  

What I want to do is save the data at the same time that   they write.

I will tell you that you can save the data at the time you are writing each character as you are doing, using the class TextWatcher , however in the case of a form where values are saved, it is not a good practice, the reason is simply because the data at the end is required complete, the name, the telephone, etc. The best thing is to have a view that I made the action of saving them at the end of the filling of the form.

If you want to save the value at the time of writing each character, the method used is onTextChanged() , you can use SharedPreferences, a database or any other storage method:

 @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      //valor a guardar.
      saveValue(getApplicationContext(), s.toString());
   }

As an example it can be getSharedPreferences () :

private String PREFS_KEY = "mispreferencias";

public void saveValue(Context context, String text) {
    SharedPreferences settings = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    SharedPreferences.Editor editor;
    editor = settings.edit();
    editor.putString("valorEditText", text);
    editor.commit();
}



public String getValue(Context context) {
    SharedPreferences preferences = context.getSharedPreferences(PREFS_KEY, MODE_PRIVATE);
    return  preferences.getString("valorEditText", "");
}

so that I loaded the value at startup simply loads the value obtained from the preferences, by means of the suggested getValue() method, this could be within onCreate() of your Activity :

  editText.setText(getValue(getApplicationContext()));
    
answered by 11.04.2017 в 22:28