12
Développer sur Android Android Lab Test www.AndroidLabTest.com Facebook Par Bruno Delb www.youtube.com/androidlabtest www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com www.facebook.com/Androidlabtest Youtube Site officiel Leçon : Les préférences partagées

Android Lab Test : Le stockage avec SharedPreferences (français)

Embed Size (px)

Citation preview

Page 1: Android Lab Test : Le stockage avec SharedPreferences (français)

Développer sur AndroidAndroid Lab Test

www.AndroidLabTest.com

Face

bo

ok

Par Bruno Delb

www.youtube.com/androidlabtest

www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com

www.facebook.com/Androidlabtest

You

tub

e

Site

off

icie

l

Leçon : Les préférences partagées

Page 2: Android Lab Test : Le stockage avec SharedPreferences (français)

Les préférences partagées

• Dans cette leçon, vous allez apprendre à

stocker des préférences partagées.

• Pour cela, vous allez utiliser le

SharedPreferences.

Page 3: Android Lab Test : Le stockage avec SharedPreferences (français)

Les préférences partagées

• Ouvrez les préférences partagées « demo » en mode privé :

SharedPreferences sharedPreferences = getSharedPreferences ("demo", Context.MODE_PRIVATE);

• Pour lire les préférences, utilisez la méthode getString() pour les

chaînes de caractères, getBoolean pour les booléens, etc …

et_username.setText (sharedPreferences.getString("username", ""));

cb_keepSession.setChecked (sharedPreferences.getBoolean ("keepSession", false));

Page 4: Android Lab Test : Le stockage avec SharedPreferences (français)

Les préférences partagées

• Pour écrire les valeurs dans les préférences partagées, appelez la méthode

edit() pour entrer en mode édition.Editor editor = sharedPreferences.edit();

• Puis les méthodes putString(), putBoolean(), etc … selon le type de chaque

information.

editor.putString("username", et_username.getText().toString());

editor.putString("password", et_password.getText().toString());

editor.putBoolean("keepSession", cb_keepSession.isChecked());

• Puis appelez la méthode commit() pour valider les données.editor.commit();

Page 5: Android Lab Test : Le stockage avec SharedPreferences (français)

Layout main.xml

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

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="5dip"

android:text="Username:" />

<EditText

android:id="@+id/et_username"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:singleLine="true" />

Page 6: Android Lab Test : Le stockage avec SharedPreferences (français)

Layout main.xml

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="5dip"

android:text="Password:" />

<EditText

android:id="@+id/et_password"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:singleLine="true" />

Page 7: Android Lab Test : Le stockage avec SharedPreferences (français)

Layout main.xml

<CheckBox

android:id="@+id/cb_keepSession"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Keep session ?" />

<Button

android:id="@+id/submit"

android:text="Submit"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

</LinearLayout>

Page 8: Android Lab Test : Le stockage avec SharedPreferences (français)

Fichier Main.java

public class Main extends Activity {

EditText et_username;

EditText et_password;

CheckBox cb_keepSession;

SharedPreferences sharedPreferences;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

et_username = (EditText)findViewById(R.id.et_username);

et_password = (EditText)findViewById(R.id.et_password);

cb_keepSession = (CheckBox)findViewById(R.id.cb_keepSession);

Button btnSubmit = (Button)findViewById(R.id.submit);

Page 9: Android Lab Test : Le stockage avec SharedPreferences (français)

Fichier Main.java

btnSubmit.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Editor editor = sharedPreferences.edit();

editor.putString("username", et_username.getText().toString());

editor.putString("password", et_password.getText().toString());

editor.putBoolean("keepSession", cb_keepSession.isChecked());

editor.commit();

finish();

}

});

sharedPreferences = getSharedPreferences ("demo", Context.MODE_PRIVATE);

et_username.setText (sharedPreferences.getString("username", ""));

et_password.setText (sharedPreferences.getString("password", ""));

cb_keepSession.setChecked (sharedPreferences.getBoolean ("keepSession", false));

Page 10: Android Lab Test : Le stockage avec SharedPreferences (français)

Fichier Main.java

}

public void onResume() {

super.onResume();

}

public void onPause() {

super.onPause();

}

}

Page 11: Android Lab Test : Le stockage avec SharedPreferences (français)

Testez sur votre mobile

Storage_SharedPreferences

Page 12: Android Lab Test : Le stockage avec SharedPreferences (français)

Retrouvez-moi sur ma chaîne AndroidLabTest …

Sur ma chaîne Youtubehttps://www.youtube.com/user/mobiledevlabtest

Qui suis-je ?

Bruno Delb,

auteur du 1er livre francophone de développement d’application Java sur mobile (2002),

développeur d’applications mobiles & sociales,

parlez-moi de vos projets.

Et bien sûr sur mon site Web :http://blog.brunodelb.com