Save multiple EditText values using SharedPreferen

2020-05-10 06:28发布

问题:

I'm trying to build an app where someone can fill in their personal data like their name, telephone number, Email...

For each field mentioned above, I created an EditText. Now my goal is to save the user's input using SharedPreferences so that he/she don't have to fill it up every time they reopen the app.

The codes I've found take care of saving the data for one EditText only (Save entered text in editText via button). How can I achieve that on multiple EditText fields?

This is my XML file below:

    <?xml version="1.0" encoding="utf-8"?>

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vul dit compleet in"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:textStyle="bold"/>

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Naam: "
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:textStyle="bold"/>


    <EditText 
        android:id="@+id/edit_Naam"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:hint="Vul uw naam in"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Functie: "
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:textStyle="bold"/>


    <EditText 
        android:id="@+id/edit_Functie"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:hint="Vul uw functie in"/>

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Personeelsnummer "
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:textStyle="bold"/>


    <EditText 
        android:id="@+id/edit_Plnr"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:hint="Vul uw plnr in"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email: " 
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:textStyle="bold"/>

    <EditText 
        android:id="@+id/edit_Email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:hint="NS mail"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Telefoon: "  
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:textStyle="bold"/>

     <EditText
         android:id="@+id/edit_Telefoon"  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number" 
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#000066"
        android:hint="zonder +31"/>





    <Button
        android:id="@+id/button_Opslaan"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/edit_Bericht"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="48dp"
            android:layout_weight="0.03"
            android:background="#009CDE"
            android:drawableLeft="@drawable/ic_launcher"
            android:text="Opslaan"
            android:textColor="#FFFFFF"/>


</LinearLayout>
</ScrollView>

回答1:

You just have to save each EditText value and retrieve them next time your Activity reloads. The code below is adapted from the link you mentioned in your question:

public class PersonalInformation extends Activity{

private SharedPreferences savedFields;
private Button saveButton;
private EditText editText1;
private EditText editText2;
// Add all your EditTexts...

// Upon creating your Activity, reload all the saved values.
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    saveButton = (Button) findViewById(R.id.your_save_button_id);
    editText1 = (EditText) findViewById(R.id.your_edit_text_1_id);
    editText2 = (EditText) findViewById(R.id.your_edit_text_2_id);
    // Keep adding all your EditTexts the same way...

    // "info" is just a tag name, use anything you like
    savedFields = getSharedPreferences("info", MODE_PRIVATE);

    // In case no value is already saved, use a Default Value
    editText1.setText(savednotes.getString("editText1", "Default Value 1"));
    editText2.setText(savednotes.getString("editText2", "Default Value 2"));

    // Save the changes upon button click
    saveButton.setOnClickListener(saveButtonListener);
}

public OnClickListener saveButtonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        SharedPreferences.Editor preferencesEditor = savedFields.edit();
        if(editText1.getText().length() > 0) // Not empty
             preferencesEditor.putString("editText1", editText1.getText());
        if(editText2.getText().length() > 0) // Not empty
             preferencesEditor.putString("editText2", editText2.getText());
        // You can make a function so you woudn't have to repeat the same code for each EditText

        // At the end, save (commit) all the changes
        preferencesEditor.commit();
        }
    }
};
}


回答2:

Try this to store in shared preferences

   // MY_CONTAINER - a static String variable like: 
    //public static final String MY_CONTAINER  = "MyPrefsFile";
    SharedPreferences.Editor editor = getSharedPreferences(MY_CONTAINER , MODE_PRIVATE).edit();
     editor.putString("edittext1", "value1"); // you can also use like this editor.putString("edittext1", ed1.getText().toString())
     editor.putString("edittext2", "value2");
editor.putString("edittext3", "value3");
editor.putString("edittext4", "value4");
editor.putString("edittext5", "value5");
     editor.commit();

To retrieve

SharedPreferences prefs = getSharedPreferences(MY_CONTAINER, MODE_PRIVATE); 
  String edit1 = prefs.getString("edittext1");
  String edit2 = prefs.getString("edittext2");
  String edit3 = prefs.getString("edittext3");
  String edit4 = prefs.getString("edittext4");
  String edit5 = prefs.getString("edittext5");