XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- Champ pour le protocole -->
<EditText
android:id="@+id/etProtocole"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Protocole" />
<!-- Champ pour le serveur -->
<EditText
android:id="@+id/etServeur"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Serveur" />
<!-- Champ pour le numéro de port -->
<EditText
android:id="@+id/etPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="N° Port"
android:inputType="number" />
<!-- Champ pour la page -->
<EditText
android:id="@+id/etPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Page" />
<!-- Bouton pour enregistrer -->
<Button
android:id="@+id/btnEnregistrer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enregistrer" />
<!-- Bouton pour afficher -->
<Button
android:id="@+id/btnAfficher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Afficher" />
</LinearLayout>
Java
package com.example.parametrageapp;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class ParametrageActivity extends AppCompatActivity {
private EditText etProtocole, etServeur, etPort, etPage;
private Button btnEnregistrer, btnAfficher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parametrage);
// Liaison des vues
etProtocole = findViewById(R.id.etProtocole);
etServeur = findViewById(R.id.etServeur);
etPort = findViewById(R.id.etPort);
etPage = findViewById(R.id.etPage);
btnEnregistrer = findViewById(R.id.btnEnregistrer);
btnAfficher = findViewById(R.id.btnAfficher);
// Gestion du clic sur "Enregistrer"
btnEnregistrer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Enregistrer les valeurs dans les préférences partagées
SharedPreferences preferences = getSharedPreferences("Parametres",
MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Protocole", etProtocole.getText().toString());
editor.putString("Serveur", etServeur.getText().toString());
editor.putString("Port", etPort.getText().toString());
editor.putString("Page", etPage.getText().toString());
editor.apply();
Toast.makeText(ParametrageActivity.this, "Paramètres enregistrés",
Toast.LENGTH_SHORT).show();
}
});
// Gestion du clic sur "Afficher"
btnAfficher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Charger les valeurs des préférences partagées
SharedPreferences preferences = getSharedPreferences("Parametres",
MODE_PRIVATE);
String protocole = preferences.getString("Protocole", "N/A");
String serveur = preferences.getString("Serveur", "N/A");
String port = preferences.getString("Port", "N/A");
String page = preferences.getString("Page", "N/A");
// Afficher les valeurs dans un Toast
String message = "Protocole: " + protocole + "\n" +
"Serveur: " + serveur + "\n" +
"Port: " + port + "\n" +
"Page: " + page;
Toast.makeText(ParametrageActivity.this, message,
Toast.LENGTH_LONG).show();
}
});
}
}