voici mon code java (enfin le tien) :
package com.example.montestparametre;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public String txt1, txt2;
public EditText string1, string2;
public SharedPreferences sharedpreferences ;
public final static String TXT1="", TXT2="";
public static final String filepref = "filepref";
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
string1 = (EditText) findViewById(R.id.editText1);
string2 = (EditText) findViewById(R.id.editText2);
sharedpreferences = getSharedPreferences(filepref, Context.MODE_PRIVATE);
}
public void sauvegarde_txt(View v) {
String s1 = string1.getText().toString();
String s2 = string2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(TXT1, s1);
editor.putString(TXT2, s2);
editor.commit();
}
public void recuperation_txt(View v) {
string1 = (EditText) findViewById(R.id.editText1);
string2 = (EditText) findViewById(R.id.editText2);
sharedpreferences = getSharedPreferences(filepref, Context.MODE_PRIVATE);
if (sharedpreferences.contains(TXT1)) {
string1.setText(sharedpreferences.getString(TXT1, ""));
}
if (sharedpreferences.contains(TXT2)) {
string2.setText(sharedpreferences.getString(TXT2, ""));
}
}
}
et ma vue xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="
"
xmlns:app="
"
xmlns:tools="
"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="207dp"
android:layout_height="87dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="99dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="230dp"
android:layout_height="79dp"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintTop_toBottomOf="@+id/editText1"
tools:layout_editor_absoluteX="90dp" />
<Button
android:id="@+id/button_svg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android
nClick="sauvegarde_txt"
android:text="Button svg"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
<Button
android:id="@+id/button_recup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android
nClick="recuperation_txt"
android:text="Button Recup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>